Home  >  Article  >  Backend Development  >  How to use database query language in PHP to process and operate database-related data types

How to use database query language in PHP to process and operate database-related data types

WBOY
WBOYOriginal
2023-07-15 22:01:071339browse

How to use database query language in PHP to process and manipulate database-related data types

Database Query Language (SQL) is a standardized language for processing data in a database. In PHP, we can use various SQL query statements to process and operate database-related data types. This article will introduce how to use SQL in PHP to process and manipulate data types in the database, and provide corresponding code examples.

  1. Connect to the database

First, we need to connect to the database through PHP code. You can use extensions such as mysqli or PDO to implement database connections. The following is a sample code that uses the mysqli extension to connect to a MySQL database:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

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

// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>
  1. Insert data

Once the connection is successful, we can use the SQL INSERT statement to insert into the database data. The following is a sample code that demonstrates how to insert data into a table named "users":

<?php
$sql = "INSERT INTO users (username, password, email) VALUES ('john', '123456', 'john@example.com')";

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

$conn->close();
?>
  1. Querying data

Querying data in the database is the most common One of the operations. We can use the SELECT statement to obtain data under specific conditions. Here is a sample code that demonstrates how to query data in the "users" table:

<?php
$sql = "SELECT id, username, email FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"] . " - 用户名: " . $row["username"] . " - 邮箱: " . $row["email"] . "<br>";
    }
} else {
    echo "0 结果";
}

$conn->close();
?>
  1. Update data

Updating data in the database is another common operation. We can use UPDATE statement to update data in the database. The following is a sample code that demonstrates how to update data in the "users" table:

<?php
$sql = "UPDATE users SET password='654321' WHERE username='john'";

if ($conn->query($sql) === true) {
    echo "数据更新成功";
} else {
    echo "数据更新失败: " . $conn->error;
}

$conn->close();
?>
  1. Delete data

Deleting data in the database is also a very common operation. We can use DELETE statement to delete data in the database. The following is a sample code that demonstrates how to delete data in the "users" table:

<?php
$sql = "DELETE FROM users WHERE username='john'";

if ($conn->query($sql) === true) {
    echo "数据删除成功";
} else {
    echo "数据删除失败: " . $conn->error;
}

$conn->close();
?>

Summary:

This article introduces how to use SQL in PHP to process and operate database-related data types . Through sample codes for operations such as connecting to the database, inserting data, querying data, updating data, and deleting data, you can better understand how to use SQL to process and operate data types in the database. Hope this article helps you!

The above is the detailed content of How to use database query language in PHP to process and operate database-related data types. 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