How to query the database and output query results in PHP

PHPz
Release: 2023-03-24 15:26:01
Original
2019 people have browsed it

PHP is a popular programming language that is often used for server-side development. In PHP development, one of the most common operations is querying the database, because most websites need to read data from the database and present it to the user. When querying the database, developers usually need to output query statements to facilitate debugging and troubleshooting.

This article will introduce in detail the methods and techniques of PHP query database output query statement.

  1. Connect to the database

Before you start querying the database, you need to connect to the database first. In PHP, you can use extension libraries such as mysqli or PDO to connect to the database. Taking mysqli as an example, the connection code is as follows:

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

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

// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>
Copy after login

Among them, $servername, $username, $password and $dbname respectively represent the server name, user name, password and database name. If the connection is successful, "Connection successful" will be output, otherwise the reason for the connection failure will be output.

  1. Constructing a query statement

After connecting to the database, you can construct a query statement. The structure of the query statement is as follows:

SELECT column1, column2, ... FROM table_name WHERE condition1 [AND [OR]] condition2 ...
Copy after login

Among them, column1, column2, etc. represent the columns that need to be queried, table_name represents the table that needs to be queried, condition1 and condition2, etc. represent the query conditions. For example, to query all records with the country "China" in a table named "customers", you can use the following code:

$sql = "SELECT * FROM customers WHERE Country='China'";
Copy after login
  1. Execute the query statement

After constructing the query statement, you can execute the query statement. In PHP, you can use the mysqli_query function to execute the query statement. The code is as follows:

$result = mysqli_query($conn, $sql);
Copy after login

Among them, $conn represents the database connection and $sql represents the query statement. After successful execution, $result will save the query results. If execution fails, false will be returned.

  1. Output query results

Output query statements are very useful when debugging and troubleshooting. In PHP, you can use the mysqli->real_escape_string function to escape special characters in the query statement, and use the echo function to output the query statement to the screen. The complete code is as follows:

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

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

// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 构造查询语句
$sql = "SELECT * FROM customers WHERE Country='" . $conn->real_escape_string('China') . "'";

// 输出查询语句
echo $sql;

// 执行查询语句
$result = mysqli_query($conn, $sql);

// 检查查询结果
if (mysqli_num_rows($result) > 0) {
    // 输出数据
    while($row = mysqli_fetch_assoc($result)) {
        echo "ID: " . $row["CustomerID"]. " - Name: " . $row["CustomerName"]. " - Country: " . $row["Country"]. "<br>";
    }
} else {
    echo "0 结果";
}

mysqli_close($conn);
?>
Copy after login

In this code, the query statement is first constructed, then the real_escape_string function is used to escape and safely process the query conditions, and then the query statement is output using the echo function and the query statement is executed. . Finally, use the mysqli_fetch_assoc function to fetch the query results row by row and output them to the screen.

  1. Summary

In PHP development, querying the database is one of the most common operations. Outputting query statements can help developers better analyze query results and troubleshoot problems. In this article, we introduced how to connect to the database, construct query statements, execute query statements, and output query statements. Hope this helps PHP developers.

The above is the detailed content of How to query the database and output query results in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template