How to use MySQL database in php?

王林
Release: 2024-03-27 08:40:02
Original
879 people have browsed it

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 "连接成功";
?>
Copy after login

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 "没有数据";
}
Copy after login
  1. Insert data
$sql = "INSERT INTO users (name, age) VALUES ('Tom', 25)";
if ($conn->query($sql) === TRUE) {
    echo "插入数据成功";
} else {
    echo "插入数据失败: " . $conn->error;
}
Copy after login
  1. Update data
$sql = "UPDATE users SET age = 26 WHERE id = 1";
if ($conn->query($sql) === TRUE) {
    echo "更新数据成功";
} else {
    echo "更新数据失败: " . $conn->error;
}
Copy after login
  1. Delete data
$sql = "DELETE FROM users WHERE id = 1";
if ($conn->query($sql) === TRUE) {
    echo "删除数据成功";
} else {
    echo "删除数据失败: " . $conn->error;
}
Copy after login

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();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template