Home  >  Article  >  Backend Development  >  How to manipulate database data using PHP built-in functions?

How to manipulate database data using PHP built-in functions?

WBOY
WBOYOriginal
2024-04-22 11:12:02830browse

PHP provides built-in functions for interacting with databases and data operations, such as connecting to databases, creating tables, inserting, reading, updating, and deleting data. These functions simplify the process of interacting with the database, making it easy to manage data in your application.

如何使用 PHP 内置函数操纵数据库数据?

Use PHP built-in functions to manipulate database data

PHP provides a rich set of built-in functions that can be used to interact with the database and manipulate data . These functions simplify the process of creating, reading, updating, and deleting data from the database. This article will introduce some commonly used PHP database manipulation functions and provide code examples to demonstrate their usage.

Database connection

Before using the database function, you need to establish a connection with the database first. This is done using extensions such as mysqli_connect() or PDO. Here is an example:

$servername = "localhost";
$username = "username";
$password = "password";
$database = "database_name";

// MySQLi
$conn = mysqli_connect($servername, $username, $password, $database);

// PDO
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);

Create data table

CREATE TABLE statement is used to create a new data table. It requires specifying the name and column structure of the table.

$sql = "CREATE TABLE users (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
)";

if (mysqli_query($conn, $sql)) {
    echo "Table users created successfully";
} else {
    echo "Error creating table: " . mysqli_error($conn);
}

Insert data

INSERT INTO statement is used to insert data into the table. It requires specifying the table name, column names and the values ​​to be inserted.

$name = "John Doe";
$email = "john.doe@example.com";

$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";

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

Reading data

The SELECT statement is used to retrieve data from a table. It can specify the columns to retrieve, the table name, and an optional WHERE condition.

$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // 输出结果集中的每行
    while($row = mysqli_fetch_assoc($result)) {
        echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
    }
} else {
    echo "No records found";
}

Update data

UPDATE statement is used to update existing data in the table. It requires specifying the table name, the column to set the new value on, and an optional WHERE condition.

$id = 1;
$name = "Jane Doe";
$email = "jane.doe@example.com";

$sql = "UPDATE users SET name='$name', email='$email' WHERE id=$id";

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

Delete data

DELETE FROM statement is used to delete data from the table. It requires specifying the table name and optional WHERE condition.

$id = 1;

$sql = "DELETE FROM users WHERE id=$id";

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

These are just a few examples of the many built-in functions in PHP for manipulating database data. By understanding the use of these functions, developers can easily interact with the database and manage data in their applications.

The above is the detailed content of How to manipulate database data using PHP built-in functions?. 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