Home Backend Development PHP Problem How to query a single database using PHP

How to query a single database using PHP

Apr 12, 2023 pm 02:44 PM

PHP is a popular programming language used for developing dynamic web applications. When developing web applications, you generally need to access databases to obtain, store, and manage data. PHP makes it easy to connect to databases and perform various types of queries.

In PHP, use one of MySQLi and PDO to interact with the database. This article explains how to query a single database using PHP. Here are the steps:

Step 1: Connect to the database

In PHP, you need to connect to the database to execute the query. Connect to the database using the following line of code:

$servername = "localhost"; //数据库地址
$username = "username"; //数据库用户名
$password = "password"; //数据库密码
$dbname = "myDB"; //数据库名称

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

// 检查连接是否成功
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
Copy after login

In the above example, we have localhost as the database address, myDB as the database name, and username and password are the username and password of the database respectively. If the connection is unsuccessful, a "Connection failed" message will be printed. If the connection is successful, an object named $conn will be created that you can use for queries.

Step 2: Execute the query statement

Once connected to the database, you can start executing the query. Use the following line of code to get the data for the id, name, and email columns from table users:

$sql = "SELECT id, name, email FROM users";
$result = mysqli_query($conn, $sql);
Copy after login

in In the above example, we use the $sql variable to store the SQL query statement. mysqli_query() The function accepts the $conn object and query statement created when connecting to the database above as parameters, and stores the query results in the $result variable.

Step 3: Process the query results

Once the query statement is executed, you can process the query results. Use the following line of code to loop through each row of data in the $result variable and print out its id, name, and email values:

// 输出数据
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 "0 结果";
}
Copy after login

In the above example, we use the mysqli_num_rows() function to determine whether there is data in the result set. If there is data in the result set, use while to loop through all rows and use the mysqli_fetch_assoc() function to get the data of the current row. Within the loop, use the echo statement to print out the id, name, and email values ​​for each row.

Step 4: Close the connection

When you have finished processing the query results, you can close the connection to the database. Close the connection using the following line of code:

mysqli_close($conn);
Copy after login

In the above example, we close the $conn object using the mysqli_close() function. This will disconnect from the database.

Summary

The above is the complete process of querying a single database using PHP. Please note that this is just a basic example, you can use various types of queries and different query statements to retrieve and update data in the database. Once you become familiar with this process, you can start building more complex web applications and interacting with databases.

The above is the detailed content of How to query a single database using PHP. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24