Home >Backend Development >PHP Tutorial >How to use MySQL database in php?

How to use MySQL database in php?

王林
王林Original
2024-03-27 08:39:04913browse

How to use MySQL database in php?

PHP is a popular web programming language, and MySQL database is a commonly used relational database management system. The combination of the two can provide powerful data storage and management functions for our web applications. This article will introduce how to use PHP to connect and operate a MySQL database.

1. Connect to MySQL database

Before using PHP to connect to the MySQL database, we need to install the MySQL extension first. Before PHP 5.3, you can use the mysql extension, and in PHP 5.3 and above, it is recommended to use the mysqli extension.

The code example for using the mysqli extension to connect to the MySQL database is as follows:

<?php
// 数据库连接参数
$host = 'localhost';
$user = 'root';
$password = '123456';
$database = 'test';

// 创建连接
$conn = new mysqli($host, $user, $password, $database);

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

2. Execute the MySQL query statement

After connecting to the MySQL database, we can execute various Various MySQL query statements. The following are sample codes for some commonly used MySQL query statements:

  1. Query data
$sql = "SELECT * FROM users";  // 查询users表中的所有数据
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - 姓名: " . $row["name"]. " - 年龄: " . $row["age"]. "<br>";
    }
} else {
    echo "没有数据";
}
  1. Insert data
$sql = "INSERT INTO users (name, age) VALUES ('Tom', 25)";
if ($conn->query($sql) === TRUE) {
    echo "插入数据成功";
} else {
    echo "插入数据失败: " . $conn->error;
}
  1. Update data
$sql = "UPDATE users SET age = 26 WHERE id = 1";
if ($conn->query($sql) === TRUE) {
    echo "更新数据成功";
} else {
    echo "更新数据失败: " . $conn->error;
}
  1. Delete data
$sql = "DELETE FROM users WHERE id = 1";
if ($conn->query($sql) === TRUE) {
    echo "删除数据成功";
} else {
    echo "删除数据失败: " . $conn->error;
}

3. Close the database connection

After completing the operation of the MySQL database, we need to close the database connection . This can be achieved through the mysqli_close() function. The code example is as follows:

// 关闭连接
$conn->close();

Summary:

This article introduces how to use PHP to connect and operate a MySQL database. We learned how to connect to a MySQL database, execute queries, insert, update, and delete data, and close database connections. These operations can help us provide powerful data storage and management capabilities for web applications.

The above is the detailed content of How to use MySQL database in php?. 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