Home  >  Article  >  Backend Development  >  Summary of database management skills in PHP development public account

Summary of database management skills in PHP development public account

PHPz
PHPzOriginal
2023-09-20 09:22:48864browse

Summary of database management skills in PHP development public account

Summary of database management skills in PHP development of public accounts

Abstract:
With the rapid development of the mobile Internet, public accounts have become an important link between enterprises and users. One of the important channels of communication. In the development of public accounts, database management is a very critical part. This article will summarize some database management skills in PHP development public accounts and give specific code examples to help developers better understand and apply them.

1. Connect to the database
In PHP, we can use mysqli or PDO extension to connect to the database. Examples are as follows:

// Use mysqli extension to connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$ dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {

die("连接数据库失败:" . $conn->connect_error);

}

// Use PDO extension to connect to the database
$dsn = "mysql:host=localhost;dbname=database";
$username = "username";
$password = " password";

try {

$conn = new PDO($dsn, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) {

die("连接数据库失败:" . $e->getMessage());

}

2. Execute SQL statement
in PHP When developing official accounts, we often need to execute SQL statements to operate the database, including inserts, updates, deletes, and queries. An example is as follows:

//Insert data
$sql = "INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')";
$result = $conn->query($sql);
if ($result === true) {

echo "插入数据成功";

} else {

echo "插入数据失败:" . $conn->error;

}

/ / Update data
$sql = "UPDATE users SET name='Jane Doe' WHERE id=1";
$result = $conn->query($sql);
if ($result = == true) {

echo "更新数据成功";

} else {

echo "更新数据失败:" . $conn->error;

}

//Delete data
$sql = "DELETE FROM users WHERE id=1";
$result = $conn->query($sql);
if ($result === true) {

echo "删除数据成功";

} else {

echo "删除数据失败:" . $conn->error;

}

//Query data
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {

while ($row = $result->fetch_assoc()) {
    echo "姓名:" . $row["name"] . ",邮箱:" . $row["email"];
}

} else {

echo "没有查询到数据";

}

3. Prevent SQL injection attacks
In order to prevent SQL injection attacks when processing user-entered data, We should use prepared statements or use mysqli's real_escape_string function to process the input data. Examples are as follows:

// Using prepared statements
$sql = "INSERT INTO users (name, email) VALUES (?, ?)";
$stmt = $conn->prepare ($sql);
$stmt->bind_param("ss", $name, $email);

$name = "John Doe";
$email = "john@example .com";
$stmt->execute();

// Use real_escape_string function
$name = $conn->real_escape_string($_POST["name"]);
$email = $conn->real_escape_string($_POST["email"]);

$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email' )";
$result = $conn->query($sql);

4. Close the database connection
After using the database, we need to close the database connection to release resources. An example is as follows:

$conn->close(); // Use mysqli extension to close the database connection
unset($conn); // Use PDO extension to close the database connection

Conclusion :
This article summarizes the database management skills in PHP development public accounts, and gives specific code examples, including connecting to the database, executing SQL statements, preventing SQL injection attacks, and closing database connections. Developers can flexibly use these techniques according to the actual situation to ensure safe and efficient database operations of public accounts.

The above is the detailed content of Summary of database management skills in PHP development public account. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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