Home > Backend Development > PHP Tutorial > Database connection and operation -- using MySQL in web applications

Database connection and operation -- using MySQL in web applications

WBOY
Release: 2023-09-10 13:00:01
Original
1791 people have browsed it

数据库连接与操作 -- 在Web应用中使用MySQL

1. Overview
The database is an integral part of Web applications. It is used to store and manage data. MySQL is one of the most popular relational databases currently and has a wide range of applications. In this article, we will focus on how to use MySQL for database connections and operations in web applications.

2. Database connection
To use MySQL in a web application, you first need to establish a database connection. In PHP language, you can use the mysqli extension library to implement database connection. The following is a sample code:

<?php
$servername = "localhost"; //数据库主机名
$username = "username"; //数据库用户名
$password = "password"; //数据库密码
$dbname = "dbname"; //数据库名

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

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

echo "数据库连接成功";
?>
Copy after login

In the above code, we first define the host name, user name, password and database name of the database. Then use the constructor of the mysqli class to create the database connection object $conn. Then use the connect_error attribute to check whether the connection is successful. If the connection fails, an error message is output and the program is terminated. If the connection is successful, "Database connection successful" is output.

3. Database operations
Once the connection is established with the database, we can perform various database operations, such as inserting, querying, updating and deleting data, etc. The following are some common examples of database operations:

  1. Insert data
    To insert data into the database, you can use the INSERT INTO statement. The following code demonstrates how to insert a student record into the database table named students:
<?php
$sql = "INSERT INTO students (name, age, gender) VALUES ('John', 18, 'Male')";

if ($conn->query($sql) === TRUE) {
    echo "数据插入成功";
} else {
    echo "数据插入失败: " . $conn->error;
}
?>
Copy after login

In the above code, we use INSERT INTO## The # statement specifies the table and data values ​​into which data is to be inserted. If the insertion is successful, "data insertion successful" is output; if the insertion fails, specific error information is output.

    Query data
  1. To query data from the database, you can use the
    SELECT statement. The following is a simple example:
  2. <?php
    $sql = "SELECT * FROM students";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        //输出每一行数据
        while ($row = $result->fetch_assoc()) {
            echo "姓名:" . $row["name"] . ",年龄:" . $row["age"] . ",性别:" . $row["gender"] . "<br>";
        }
    } else {
        echo "没有查询到数据";
    }
    ?>
    Copy after login
In the above code, we use

SELECT * FROM students to query all the data in the students table. If the query result is not empty, each row of data will be output through loop traversal; if the query result is empty, "No data found" will be output.

    Update data and delete data
  1. To update data, you can use the
    UPDATE statement; to delete data, you can use the DELETE statement. The following are two simple examples:
  2. //更新数据
    $sql = "UPDATE students SET age = 20 WHERE id = 1";
    
    //删除数据
    $sql = "DELETE FROM students WHERE id = 1";
    Copy after login
    In the above code, we use the

    UPDATE statement to update the student's ID of 1 in the students table The age is updated to 20; the student with id 1 is deleted using the DELETE statement.

    4. Summary

    It is very common to use MySQL for database connections and operations in web applications. This article describes how to use the MySQL extension to establish a database connection and demonstrates sample code for inserting, querying, updating, and deleting data. We hope that through the introduction of this article, readers can better understand and apply the relevant knowledge of database connections and operations.

    The above is the detailed content of Database connection and operation -- using MySQL in web applications. 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