PHP MySQL connection
For PHP 5 and above, it is recommended to use the following method to connect to MySQL:
MySQLi extension ("i" means improved)
PDO (PHP Data Objects)
In early versions of PHP we use the MySQL extension. However, this extension was deprecated in 2012.
Should I use MySQLi or PDO?
If you need a short answer, "use whichever you are comfortable with".
MySQLi and PDO have their own advantages:
PDO is used in 12 different databases, and MySQLi is only for MySQL databases.
So, if your project needs to switch between multiple databases, it is recommended to use PDO, so that you only need to modify the connection string and department query statement. With MySQLi, if you use a different database, you need to rewrite all code, including queries.
Both are object-oriented, but MySQLi also provides an API interface.
Both support prepared statements. Prepared statements can prevent SQL injection and are very important for the security of web projects.
MySQLi and PDO connect to MySQL instance
In this chapter and the following chapters, we will use the following three methods to demonstrate PHP operating MySQL:
MySQLi (object-oriented)
MySQLi (procedure-oriented)
PDO
MySQLi installation
Linux and Windows: In most cases, the MySQLi extension is automatically installed when the php5 mysql package is installed.
For installation details, please check: http://php.net/manual/en/mysqli.installation.php
You can check whether the installation is successful through phpinfo():
PDO Installation
For installation details, please see: http://php.net/manual/en/pdo.installation.php
You can check whether the installation is successful through phpinfo():
PHP connects to MySQL
Before we access the MySQL database, we need to first Connect to the database server:
Instance (MySQLi - Object Oriented)
<?php $servername = "localhost"; $username = "username"; $password = "password"; // 创建连接 $conn = new mysqli($servername, $username, $password); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } echo "连接成功"; ?>
Note that $connect_error in the above object-oriented example was added in PHP 5.2.9 and 5.3.0. If you need compatibility with earlier versions
Please use the following code to replace: //Detect connection if (mysqli_connect_error()) { die("Database connection failed: " . mysqli_connect_error()); } |
Example (MySQLi - Procedure Oriented)
<?php $servername = "localhost"; $username = "username"; $password = "password"; // 创建连接 $conn = mysqli_connect($servername, $username, $password); // 检测连接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "连接成功"; ?
Example (PDO)
<?php $servername = "localhost"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); echo "连接成功"; } catch(PDOException $e) { echo $e->getMessage(); } ?>
Note that we have specified the database (myDB) in the above PDO example. PDO needs to set the database name during the connection process. If not specified, an exception will be thrown. |
Close the connection
The connection will be automatically closed after the script is executed. You can also use the following code to close the connection:
Instance(MySQLi - Object Oriented)
$conn->close();
Instance(MySQLi - Object Oriented Process)
mysqli_close($conn);
Instance (PDO)
$conn = null;
Related video tutorial recommendation: "mysql tutorial》//m.sbmmt.com/course/list/51.html
##