Home > Backend Development > PHP Tutorial > What are the ways to connect to the database in PHP?

What are the ways to connect to the database in PHP?

一个新手
Release: 2023-03-15 18:42:01
Original
32663 people have browsed it

What are the ways to connect to the database in PHP?

Common methods to connect to the database in php

##1. Use Mysql_connect() to connect to the database

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, and a MySQL connection ID is returned. If it fails, FALSE is returned. , so subsequent operations can be performed.

Code example

<?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);
?>
Copy after login

Note

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

Description:

The MySQL extension is an early extension designed and developed to allow PHP applications to interact with the MySQL database. The MySQL extension provides a procedure-oriented interface and is designed for MySQL 4.1.3 or earlier.

Therefore, although this extension can interact with MySQL4.1.3 or newer database servers, it does not support some features provided by later MySQL servers. Because it is too old and unsafe, it has been completely replaced by the later mysqli;

2. Use Mysqli

This is an object-oriented database connection method. Before connecting, you need to instantiate an object, and then perform database operations through this object

Code example

<?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());
  }
Copy after login

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

Description:

mysqli extension, which we sometimes call MySQL enhanced extension, can be used to use the new advanced features in MySQL 4.1.3 or newer versions.

Its features are: object-oriented interface, prepared statement support, multi-statement execution support, transaction support, enhanced debugging capabilities, embedded service support, and preprocessing methods that completely solve the problem of SQL injection. However, it also has a disadvantage, that is, it only supports mysql database. If you don't operate other databases, this is undoubtedly the best choice.

3. Use PDO

pdo is a method added by php5 center to connect to the database

Code example


<?php
$mysql_conf = array(
    &#39;host&#39;    => &#39;127.0.0.1:3306&#39;, 
    &#39;db&#39;      => &#39;test&#39;, 
    &#39;db_user&#39; => &#39;root&#39;, 
    &#39;db_pwd&#39;  => &#39;joshua317&#39;, 
    );
$pdo = new PDO("mysql:host=" . $mysql_conf[&#39;host&#39;] . ";dbname=" . $mysql_conf[&#39;db&#39;], $mysql_conf[&#39;db_user&#39;], $mysql_conf[&#39;db_pwd&#39;]);//创建一个pdo对象
$pdo->exec("set names &#39;utf8&#39;");
$sql = "select * from user where name = ?";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(1, &#39;joshua&#39;, PDO::PARAM_STR);
$rs = $stmt->execute();
if ($rs) {
    // PDO::FETCH_ASSOC 关联数组形式
    // PDO::FETCH_NUM 数字索引数组形式
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        var_dump($row);
    }
}
 
$pdo = null;//关闭连接
?>
Copy after login

Description:

PDO is the abbreviation of PHP Data Objects and is a database abstraction layer specification in PHP applications.

PDO provides a unified API interface that allows your PHP application to not care about the specific database server system type to be connected. In other words, if you use PDO's API, you can use it whenever needed. Seamlessly switch database servers, such as from Oracle to MySQL, with only a small amount of PHP code modifications.

Its functions are similar to interfaces such as JDBC, ODBC, and DBI. Similarly, it also solves the SQL injection problem and has good security. However, it also has disadvantages. Some multi-statement execution queries are not supported (but this situation is rare).


For more related knowledge, please visit

PHP Chinese website! !

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template