Home > Backend Development > PHP Problem > How to modify MySQL records using PHP

How to modify MySQL records using PHP

PHPz
Release: 2023-04-10 13:53:32
Original
698 people have browsed it

In Web development, PHP language is widely used in server-side programming, and MySQL is one of the most commonly used relational databases. When we need to modify the records in the MySQL database, we need to use the PHP language to achieve it. This article will introduce in detail how to use PHP to modify MySQL records.

  1. Connecting to MySQL database

PHP provides some built-in functions to connect to MySQL database, the most commonly used is the mysqli_connect() function. This function needs to pass four parameters, namely MySQL server address, user name, password and connected database name. The following is an example of connecting to a MySQL database:

$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "mydb";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
Copy after login

In the above code, we use the mysqli_connect() function to connect to a database named mydb.

  1. Execute SQL statements

Once the connection is successful, we need to use SQL statements to operate records in the database. In PHP, you can use the mysqli_query() function to execute SQL statements. This function needs to pass two parameters, the first parameter is the database connection object $conn, and the second parameter is the SQL statement to be executed. The following is an example of modifying a MySQL database record:

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if (mysqli_query($conn, $sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}
Copy after login

In the above code, we use a SQL statement UPDATE to modify the lastname field of the record with id 2 in the database table MyGuests to Doe.

  1. Close the database connection

After completing the database operation, be sure to close the database connection. In PHP, you can use the mysqli_close() function to close the connection. The following is an example of closing a connection:

mysqli_close($conn);
Copy after login
  1. Complete example

Next, we will demonstrate a complete example, including connecting to the database, executing SQL statements, and closing the connection :

$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "mydb";

$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查是否连接成功
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if (mysqli_query($conn, $sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}

mysqli_close($conn);
Copy after login
  1. Summary

This article introduces how to use PHP to modify records in the MySQL database, including connecting to the database, executing SQL statements and closing the connection. I hope this article can give readers some help in actual development. It is worth noting that you must be careful when modifying database records to ensure data security and integrity.

The above is the detailed content of How to modify MySQL records using PHP. For more information, please follow other related articles on the PHP Chinese website!

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