How to modify database data with PHP PDO

PHPz
Release: 2023-04-04 09:38:01
Original
638 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

userstable, which contains a user withid1, 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

UPDATEin the SQL statement indicates updating data,SETis followed by the fields and values that need to be modified, andWHEREconstraints.

In PHP, we use PDO's

preparemethod to prepare SQL statements, use thebindParammethod to bind parameters, and useexecuteMethod 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!