Home  >  Article  >  Backend Development  >  How to query a single database using PHP

How to query a single database using PHP

PHPz
PHPzOriginal
2023-04-12 14:44:36941browse

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());
}

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);

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 结果";
}

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);

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