How to modify database data with PHP PDO

PHPz
Release: 2023-04-04 09:38:01
Original
691 people have browsed it

In PHP dynamic web application development, database operation is a very important skill. PDO is an implementation of the PHP Data Object (PHP Data Object) extension, which can operate various database systems in an object-oriented manner in PHP.

In database operations, modifying data is an operation we often need to use. Below we will demonstrate how to modify database data through PHP PDO.

  1. Prepare database connection

First, we need to connect to the database. The example of using PDO to connect to the MySQL database is as follows:

// 数据库连接信息
$host = "localhost";
$dbname = "test";
$username = "root";
$password = "password";

// 数据库连接
try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully\n";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
Copy after login

We can modify $host, $dbname, $username, ## according to the actual situation. The value of #$password.

    Modify data
Next, we can use SQL statements to modify the data in the database. Suppose we have a

users table, which contains a user with id 1, and we want to modify the user's name to "Tom".

// 修改数据
$stmt = $conn->prepare("UPDATE users SET name=:name WHERE id=:id");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':id', $id);

$name = "Tom";
$id = 1;

$stmt->execute();
Copy after login

UPDATE in the SQL statement indicates updating data, SET is followed by the fields and values ​​that need to be modified, and WHERE constraints.

In PHP, we use PDO's

prepare method to prepare SQL statements, use the bindParam method to bind parameters, and use execute Method to execute SQL statements.

    Complete example
The following is a complete example that demonstrates how to modify data in a MySQL database through PHP PDO.

// 数据库连接信息
$host = "localhost";
$dbname = "test";
$username = "root";
$password = "password";

// 数据库连接
try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully\n";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

// 修改数据
$stmt = $conn->prepare("UPDATE users SET name=:name WHERE id=:id");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':id', $id);

$name = "Tom";
$id = 1;

$stmt->execute();

echo "Data updated successfully\n";

// 断开数据库连接
$conn = null;
Copy after login
At this point, we have completed the example of modifying data in the MySQL database through PHP PDO. Through the above examples, I believe you have understood the basic operations of using PDO to modify database data.

The above is the detailed content of How to modify database data with PHP PDO. 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