PHP connects to MySQL
PHP 5 and above are recommended to use the following method to connect to MySQL:
·MySQLiextension ("i" means improved)
·PDO(PHP Data Objects)
In the PHP Early Edition we use mysql extension. However, this extension has been deprecated since 2012, because a set ofmysqlfunctions have been deprecated
I should 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 applied in 12 different databases, and MySQLI only targets MySQL database.
Therefore, if your project needs to be switched in a variety of databases, it is recommended to use PDO, so you only need to modify the connection string and the department inquiry statement. Use mysqli. If you have different databases, you need to rewrite all the
code, including query.
Both are object -oriented, but mysqli also provides the 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 the MySQL instance
In this chapter and the following chapters, we will use the following three methods Any way to demonstrate how PHP operates MySQL:
· MySQLi (object-oriented)
· MySQLi (procedure-oriented)
· PDO
Connect MySQL
##Syntax
##mysql_connect(servername,username,password);
Description | |
servername Optional. Specifies the server to connect to. The default is "localhost:3306". |
|
|
Optional. Specifies the username to log in with. The default value is the name of users with a server process. |
# Password | . Standard the password used for login. The default is "". |
Before we access the MySQL database, we need to connect to the database server first:
Instance (MySQLi - Object Oriented)
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 to be compatible with earlier versions, please use the following code replacement:
//Detect connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}
Instance (MySQLi - procedure-oriented)
Instance (PDO)
getMessage(); } ?>
Note: In the above PDO In the example 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 ();
Instance (MySQLi - process-oriented)
##mysqli_close($conn);Instance (PDO)
$conn = null;Instance
In the following example, we store the database connection in a variable ($con). If the connection fails, the "die" part will be executed; if the connection is successful, the connection will be closed after the program is executed. The above code means to connect to a server named localhost, the user name is root, the password is root, and the test database is used; if the connection fails, execute the code in die Program running result:Connection to database successful