Home  >  Article  >  Backend Development  >  Detailed explanation of PHP and MySQL database connection: zero basics to actual combat

Detailed explanation of PHP and MySQL database connection: zero basics to actual combat

WBOY
WBOYOriginal
2024-06-01 16:17:01536browse

Summary: This guide provides a comprehensive guide to connecting to a MySQL database, including preparation, connecting to the database, querying the database, iterating through results, and inserting data. Preparation: Make sure the PHP and MySQL environments are available, and create the MySQL database and tables. Connect to the database: Use the mysqli_connect function, specify the server name, user name, password and database name. Query the database: Use the mysqli_query function to perform SQL queries. Traverse the results: Use the fetch_assoc() method to traverse the query results. Insert data: Use the mysqli_query function to execute the INSERT statement.

Detailed explanation of PHP and MySQL database connection: zero basics to actual combat

Detailed explanation of PHP and MySQL database connection: zero foundation to practical practice

In web development, connecting to the database is very important. This article will take you step by step to understand how to use PHP to connect to a MySQL database, from zero basics to actual practice.

1. Preparation

  • Make sure you have PHP and MySQL environment
  • Create a MySQL database and table

2. Connect to the database

// 数据库连接参数
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

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

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
}

3. Query the database

// 查询语句
$sql = "SELECT * FROM table_name";

// 执行查询
$result = $conn->query($sql);

// 检查查询结果是否成功
if (!$result) {
    die("查询失败:" . $conn->error);
}

4. Traverse the results

// 遍历查询结果
while ($row = $result->fetch_assoc()) {
    // 输出每一行数据
    print_r($row);
}

5. Insert data

// 插入数据语句
$sql = "INSERT INTO table_name (name, email) VALUES ('John Doe', 'johndoe@example.com')";

// 执行插入语句
if ($conn->query($sql) === TRUE) {
    // 插入成功
    echo "数据已插入";
} else {
    // 插入失败
    echo "插入失败:" . $conn->error;
}

Practical case

Create a table named "users", which contains "id" , "name" and "email" columns. Then, write the following code to connect to the database, query the user, and display them on the web page:

<!DOCTYPE html>
<html>
<body>
    <?php
    // 连接数据库
    $conn = new mysqli($servername, $username, $password, $dbname);

    // 查询用户
    $sql = "SELECT * FROM users";
    $result = $conn->query($sql);

    // 遍历结果
    echo "<h1>用户列表</h1>";
    echo "<ul>";
    while ($row = $result->fetch_assoc()) {
        echo "<li>" . $row["name"] . " (" . $row["email"] . ")</li>";
    }
    echo "</ul>";

    // 关闭数据库连接
    $conn->close();
    ?>
</body>
</html>

The above is the detailed content of Detailed explanation of PHP and MySQL database connection: zero basics to actual combat. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn