Getting Started with PHP - Connecting to MySQL
PHP 5 and above versions are recommended to use the following method to connect to MySQL:
MySQLi extension ("i" means improved)
PDO (PHP Data Objects)
In In early versions of PHP we used 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():
Connect to MySQL
Before we access the MySQL database, we need to connect first To the database server:
Example (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 "连接成功"; ?>
NoteIn the above object oriented example $connect_error was added in PHP 5.2.9 and 5.3.0. If you need compatibility with earlier versions, please use the following code replacement:
// Detect connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}
Instance (MySQLi - Process Oriented)
<?php $servername = "localhost"; $username = "username"; $password = "password"; // 创建连接 $conn = mysqli_connect($servername, $username, $password); // 检测连接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "连接成功"; ?>
Instance (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 in the above PDO instance we have specified the database (myDB). 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();
Series (MySQLI -Facial process)
## This mysqli_Close ($ CONN);
# Series (PDO)
$conn = null;