PHP development skills: How to use PHP to connect to the MySQL database
Introduction:
In PHP development, connecting to the database is a very basic and important task. As one of the most commonly used relational database management systems currently, MySQL is a common choice among developers. This article will introduce how to use PHP to connect to a MySQL database and give corresponding code examples.
1. Install and configure the MySQL database
Before we begin, we need to ensure that the MySQL database has been installed and correctly configured. If you have not installed MySQL, you can go to the official website to download the latest version and follow the instructions to install it. After the installation is complete, you also need to ensure that the MySQL server is running. Additionally, you need to create a database and corresponding tables for our sample code.
2. PHP connects to MySQL database
Connecting to MySQL database in PHP is very simple. We can use the built-in MySQLi extension or PDO to operate. These two methods are introduced below.
(1) Create a connection
//设定数据库连接参数 $hostname = "localhost"; //数据库地址 $username = "root"; //数据库用户名 $password = "password"; //数据库密码 $database = "mydb"; //数据库名 //创建一个MySQLi对象并连接数据库 $conn = new mysqli($hostname, $username, $password, $database); //检查连接是否成功 if ($conn->connect_error) { die("连接失败:" . $conn->connect_error); } echo "连接成功!";
(2) Execute database query
//执行查询语句 $sql = "SELECT * FROM users"; $result = $conn->query($sql); //检查查询是否成功 if ($result->num_rows > 0) { //遍历查询结果 while($row = $result->fetch_assoc()) { echo "用户ID:" . $row["id"]. " - 用户名:" . $row["username"]."<br>"; } } else { echo "没有查询到结果!"; }
(3) Close the connection
//关闭连接 $conn->close();
(1)Create a connection
//设定数据库连接参数 $dsn = "mysql:host=localhost;dbname=mydb"; //数据库地址和名称 $username = "root"; //数据库用户名 $password = "password"; //数据库密码 try { //创建一个PDO对象并连接数据库 $conn = new PDO($dsn, $username, $password); echo "连接成功!"; } catch(PDOException $e) { echo "连接失败:" . $e->getMessage(); }
(2)Execute database query
//执行查询语句 $sql = "SELECT * FROM users"; $result = $conn->query($sql); //检查查询是否成功 if ($result->rowCount() > 0) { //遍历查询结果 while($row = $result->fetch()) { echo "用户ID:" . $row["id"]. " - 用户名:" . $row["username"]."<br>"; } } else { echo "没有查询到结果!"; }
(3)Close the connection
//关闭连接 $conn = null;
Conclusion:
Through this article, we learned how to use PHP to connect to the MySQL database. We can choose to use MySQLi extension or PDO to perform connection operations. No matter which method you choose, as long as you configure the database connection parameters correctly and perform the corresponding operations according to the sample code, you can connect and query the MySQL database. Through skilled use, we can perform database operations more efficiently and facilitate our development work.
Attachment: Please note that the database connection parameters in the above sample code should be modified according to your actual database configuration to ensure normal connection and query operations.
The above is the detailed content of PHP development tips: How to use PHP to connect to a MySQL database. For more information, please follow other related articles on the PHP Chinese website!