Three ways PHP connect to database (Mysql) and their differences

Foreword:

After the php5.3 version, there are two ways to connect to the database, one is through mysqli, the other is through PDO, and connecting to the database through mysqli can also be divided into two situations: mysqli (object-oriented) , mysqli (process-oriented).
That is, three ways:
1) PDO to connect to mysql
2) mysqli (object-oriented) to connect to the database
3) mysqli (process-oriented) to connect to the database
(In fact, there is also a connection method: use the MySQL extension. But this Extensions are not recommended since 2012.)

PDO connection example

You can first check whether your php has PDO installed through the phpinfo() command (I used php7, which is already installed by default)
if it is not installed, refer to the web page: http://php.net/manual/en/pdo. Installation.php
Insert picture description here
code example:

<?php
$servername = "localhost";
$username = "root";
$password = "root";
try {
    
    
    $conn = new PDO("mysql:host=$servername;dbname=jtsys",
        $username, $password);
    echo "连接成功";
}
catch(PDOException $e)
{
    
    
    
    echo $e->getMessage();
}
?>

(Please pay attention to change the database user name and password when using, and the selected database name (dbname)

mysqli (object-oriented) connection example

You can first check whether your php has mysqli installed through the phpinfo() command (I used php7, which is already installed by default)
if it is not installed, refer to the web page: http://php.net/manual/en/mysqli. Installation.php
Insert picture description here
code example:

<?php
$servername = "localhost";
$username = "root";
$password = "root";
// 创建连接
$conn =
new mysqli($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
    
    
    die("连接失败: " . $conn->connect_error);
}
$dbname="jtsys";
mysqli_select_db($conn,$dbname);
echo "连接成功";
?>

mysqli (process-oriented) to connect to the database

Code example:

<?php
$servername = "localhost";
$username = "root";
$password = "root";
// 创建连接
$conn = mysqli_connect($servername, 
$username, $password);
// 检测连接
if (!$conn) {
    
    
    die("Connection 
failed: " . mysqli_connect_error());
}
$dbname="jtsys";
mysqli_select_db($conn,$dbname);
echo "连接成功";
?>

The difference between the three:

1. The way to close the connection:
PDO:

$conn = null;

MySQLi (Object Oriented):

$conn->close();

MySQLi (process-oriented):

mysqli_close($conn);
  • PDO is used in 12 different databases, MySQLi only targets MySQL databases.
  • If your project needs to switch between multiple databases, it is recommended to use PDO, so you only need to modify the connection string and the department query statement. With MySQLi, if you have a different database, you need to rewrite all the code, including queries.
  • Both are object-oriented, but MySQLi also provides an API interface.
  • Both support prepared statements. Prepared statements can prevent SQL injection, which is very important for the security of web projects.
    The difference between object-oriented and process-oriented MySQLi:
    Many PHP programmers are not used to object-oriented bai programming, so the mysqli class library provides the du method for them to use. This is also convenient for some users who use mysql extension to quickly migrate to mysqli. In fact, mysqli_query() internally encapsulates the object-oriented calling process.

Guess you like

Origin blog.csdn.net/qq_45273552/article/details/109385470