Home  >  Article  >  Backend Development  >  There are several ways to modify the database in PHP

There are several ways to modify the database in PHP

angryTom
angryTomOriginal
2019-10-26 14:34:262989browse

There are several ways to modify the database in PHP

There are several ways to modify the database in PHP

In the PHP development process, we need to connect to the database to develop dynamics There are usually four ways to connect to a database on a website: Mysql-connect(),Mysqli,PDO,ADODB, we can use this How to connect to the database in 4 to modify the database will be introduced one by one next.

Mysql_connect

This is the first database connection method we encountered when we first started learning PHP. The connection result can be returned through the mysql_connect() function, then a MySQL connection identifier, returns FALSE if failed. Therefore, subsequent database modification operations can be performed.

 <?php
  $con=mysql_connect("localhost" ,"root","password")
  if($con){
    mysql_select_db("db_name",$con);
    $sql="select * from table_name where id=1";
    $result=mysql_query($sql);
    while($row=mysql_fetch_row($result)){
        echo  "$row";
    }
  }else{
    die("无法连接数据库".mysql_error());
  }
  mysql_close($con);
  ?>

This connection method is a short connection, not a long connection. If the connection is long, use mysql_pconnectct()

Mysqli

This is a The object-oriented database connection method requires instantiating an object before connecting, and then performing database operations through this object.

<?php
    $con=new mysqli("localhost","root","password","db_name");
    if(!mysqli_connect_error()){
        $sql="select * from table_name where id=1";
        $result=$con->query($sql);
    while($row=$result->fetch_row($result)){
        echo  "$row";
    }
      }else{
        die("无法连接数据库".mysql_error());
      }

mysqli is a long connection method and is more secure than mysql_connect

PDO

What is pdo, pdo is added to the php5 center The way to connect to the database,

 <?php
        $pdo=new      
        PDO(&#39;mysql:host=localhost;dbname=db_name&#39;,"root",
        $sql="select * from table_name where id=1";
       "password");
        try{
              $result=$pdo->query($sql);
              foreach($result as $k){
                    print_r($k);
              }
            }catch(PDOException $e){
                       echo $e->getMessage();
            }
    ?>

ADODB

No matter what the back-end database is, the way to access the database is the same, and developers don’t have to settle for a certain set of Database, you must learn another set of different access methods, which greatly reduces the knowledge burden of developers. Past knowledge can still be used in the future

require_once &#39;./adodb5/adodb.inc.php&#39;;
  $conn = &ADONewConnection(&#39;mysql&#39;);
  $conn->connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;,&#39;test&#39;);
  $conn->Execute("set names utf8");
  $res = $conn->Execute("select * from user");
  if (!$res){
     echo $conn->ErrorMsg();
  }else{
     var_dump($res);
   }

For more PHP related knowledge, please visitPHP中文网!

The above is the detailed content of There are several ways to modify the database in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn