Home  >  Article  >  Backend Development  >  How to build a database-driven website with PHP

How to build a database-driven website with PHP

WBOY
WBOYOriginal
2023-09-05 12:33:341348browse

如何利用 PHP 构建数据库驱动的网站

How to use PHP to build a database-driven website

With the rapid development of the Internet, building database-driven websites has become a common task for developers. As a powerful server-side scripting language, PHP has excellent database operation capabilities and simple and easy-to-use syntax. This article explains how to build a database-driven website with PHP and provides some code examples.

1. Connect to the database
Before we start building a database-driven website, we first need to connect to the database. PHP provides a variety of ways to connect to the database, the commonly used ones are MySQLi and PDO. The following is a sample code for using MySQLi to connect to the database:

<?php
$host = "localhost";
$username = "root";
$password = "123456";
$database = "mydb";

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

// 检查连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

echo "数据库连接成功";
?>

2. Execute SQL query
After the connection is successful, we can execute SQL queries to operate the database. The following is a sample code for executing a query using MySQLi:

<?php
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
    }
} else {
    echo "0 结果";
}

$conn->close();
?>

3. Insert data
In addition to querying data, we can also insert new data into the database. The following is a sample code for inserting data using MySQLi:

<?php
$name = "John Doe";
$email = "john@example.com";

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

if ($conn->query($sql) === TRUE) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

4. Update data
Sometimes we may need to update existing data in the database. The following is a sample code for updating data using MySQLi:

<?php
$id = 1;
$name = "John Smith";

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

if ($conn->query($sql) === TRUE) {
    echo "记录更新成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

5. Delete data
Finally, we can also delete data in the database. The following is a sample code for deleting data using MySQLi:

<?php
$id = 1;

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

if ($conn->query($sql) === TRUE) {
    echo "记录删除成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Summary:
Building a database-driven website using PHP is a relatively simple task, but it also requires some basic knowledge of database operations. Through the sample code provided in this article, I believe that readers have a preliminary understanding of the combination of PHP and database. In actual development, we can modify and optimize according to specific needs, leverage the powerful functions of PHP, and build an efficient and stable database-driven website.

The above is the detailed content of How to build a database-driven website with PHP. 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