PHP connects to MySQL
PHP Connect Mysql
##qp 5 or above. It is recommended to use the following ways to connect 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.
What is PDO?
is the method of operating the database.
pdo is to encapsulate the function of operating the database into a pdo class, during which security verification is done.
The difference between PDO and mysqli:
##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 only targets 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 instanceIn this chapter and the following chapters, we will use the following three methods to demonstrate PHP operation MySQL:·
· use being using using using using using using using using using using using using using ‐ ‐ ‐ ‐ ‐ ‐ MySQLi ((object-oriented))
$mysqli = mysqli_connect('localhost','username','password','database');
· PDO
$pdo = new PDO("mysql :host=localhost;dbname=database", 'username', 'password');
MySQLi installationLinux and Windows: In When the php5 mysql package is installed, the MySQLi extension is automatically installed in most cases. For installation details, please check: http://php.net/manual/en/mysqli.installation.php
You can check whether the installation is successful through phpinfo():
PDO InstallationFor installation details, please see: http://php .net/manual/en/pdo.installation.phpYou can check whether the installation is successful through phpinfo():
Connect to MySQL
Before we access the MySQL database, we need to connect to Database server:
Instance (MySQLi - Object-oriented)
$link = mysqli_connect('localhost', 'mysql_user', 'mysql_password');
The three parameters are
• Server address (domain name, IP)
• Database user name (root)
• Database password (empty)
If the connection is successful, return a database connection, otherwise return false (we can use if judgment to prompt the user whether the connection is successful)
<?php $servername = "localhost"; $username = "username"; $password = "password"; // 创建连接 $conn = new mysqli($servername, $username, $password); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } echo "连接成功"; ?>
Note that in the above object-oriented examples$ 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 already specified the database ( myDB). PDO needs to set the database name during the connection process. If not specified, an exception will be thrown.
Select database
$db_selected = mysqli_select_db($link, 'db_name');
Two parameters
• The database connection returned by mysql_connect
• Database name
Returns true if successful, false if failed
You can also select it directly when connecting Database:
$link = mysqli_connect('localhost', 'mysql_user', 'mysql_password', 'db_name');
Close the connection
The connection will be closed automatically after the script is executed. You can also use the following code to close the connection:
Instance (MySQLi - Object Oriented)
$conn->close();
Example (MySQLi - process-oriented)
mysqli_close($conn);
Example (PDO)
$conn = null;
Friendly reminder:
Remember to be sloppy and not careful when writing programs. This is really the biggest taboo in writing programs. An error or other unknown error may occur due to a wrong name or a wrong writing of the data table name. Therefore, when writing code in the future, you must be careful and not arrogant or arrogant, so that you can write good code and avoid confusion.