How to connect through phpmysql database, this chapter will explain the database connection in detail.
Connecting to MySQL
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:
// 检测连接 if (mysqli_connect_error()) { die("数据库连接失败: " . mysqli_connect_error()); }
Instance (MySQLi - process-oriented)
Instance (PDO)
getMessage();}?>
Note that In the above PDO example, we have specified the database (myDB). PDO needs to set the database name during the connection process. If not specified, an exceptionwill 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();
mysqli_close($conn);
$conn = null;
Introduction to PHP MySQL (database related knowledge)
PHP MySQL data reading operations and Method
The above is the detailed content of PHP connection MySQL related knowledge and operations. For more information, please follow other related articles on the PHP Chinese website!