Home >Database >Mysql Tutorial >How to realize php and mysql database connection

How to realize php and mysql database connection

王林
王林forward
2023-05-28 08:40:494512browse

1. Install and configure the MySQL database

Before connecting MySQL and PHP, you need to install and configure the MySQL database. If you have not installed MySQL, you can download the MySQL installation package from the official website and install it according to the instructions. Remember to add the MySQL bin directory to the environment variables to facilitate subsequent configuration and connection operations. After completing the installation of MySQL, you need to set up and configure it, such as creating users, assigning permissions, etc.

2. Prepare the PHP environment

Before connecting to MySQL, you also need to prepare the PHP environment. If you have prepared a PHP environment, if you are using a Web integrated development environment such as WAMP or XAMPP. When setting up a PHP environment manually, you must ensure that the PHP version meets the requirements and the necessary related modules have been installed. mysqlnd is an official MySQL PHP extension that can provide better MySQL support and more efficient performance. In addition to this, there is an extension to mysqli that provides more functionality and better error handling.

3. Use mysqli to connect to MySQL

Once you have prepared MySQL and PHP, and ensure that the PHP extension is installed, you can start writing PHP code. Connected to MySQL. The following is an example of using mysqli to connect to MySQL:

<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "dbname";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>

In this example, we first determined the address of the MySQL server (localhost), user name, password and database name. Then I created a connection object using the mysqli class and passed these parameters. We also added a connect_error check to ensure the connection was successful. Finally, confirm that the connection has been successfully established by outputting a message on the PHP page.

4. Use PDO to connect to MySQL

In addition to the mysqli extension, PHP also provides another standard way to connect to MySQL - PDO (PHP database object) . PDO provides richer functions and safer data processing.

The following is an example of using PDO to connect to MySQL:

<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "dbname";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // 设置 PDO 错误模式为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "连接成功"; 
}
catch(PDOException $e)
{
    echo "连接失败: " . $e->getMessage();
}
?>

In this example, we also specify the location, login name, password and database name of the MySQL server. Then I created a connection object using PDO and passed these parameters. We also added an exception handler to ensure a successful connection and prevent program crashes due to connection issues.

5. Connect to MySQL and perform query operations

Using PHP, as long as the connection to MySQL is successfully established, you can perform various query operations. You can use the query() method provided by mysqli or PDO to perform query operations. The following is a simple example of using mysqli to execute a SELECT statement:

<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "dbname";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

$sql = "SELECT * FROM orders";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "订单号:" . $row["order_id"]. " - 客户名:" . $row["customer_name"]. " - 总价:" . $row["order_total"]. "<br>";
    }
} else {
    echo "0 结果";
}
$conn->close();
?>

In this example, we execute a SELECT statement, select all the data from the data table orders, and output the data through while loop traversal . Output the query results, or "0 results" if it is empty.

The above is the detailed content of How to realize php and mysql database connection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete