When developing with PHP, you often need to query data from the database and display it on the user interface. This article will introduce how PHP queries data from the database and outputs and displays the query results.
First, you need to connect to the database. This can be done by using PHP's built-in mysqli or PDO extensions. The following is sample code to connect to a MySQL database using the mysqli extension:
// 从配置文件中获取数据库连接信息 $db_host = "localhost"; $db_user = "username"; $db_pass = "password"; $db_name = "database"; // 连接数据库 $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name); if (!$conn) { die("连接数据库失败:" . mysqli_connect_error()); }
After connecting to the database, you can use SQL statements to query data from the database table. The following is a sample code to query data with specific conditions:
// 查询具有特定条件的数据 $sql = "SELECT * FROM users WHERE age > 18"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // 遍历查询结果 while ($row = mysqli_fetch_assoc($result)) { echo "姓名:" . $row["name"] . ", 年龄:" . $row["age"]; } } else { echo "未找到符合条件的数据"; }
In the above sample code, the SQL statement gets all users in the users table who are older than 18 years old. If the query result contains some rows, iterate over the query result and output the data. If no matching row is found, a data not found message is output.
Here, the mysqli_query function is used to execute SQL statements. If the execution is successful, it returns a MySQL result object. You can then use the mysqli_num_rows function to get the number of rows in the query results. If the query results contain any rows, you can use the mysqli_fetch_assoc function to fetch the data for each row as an associative array.
Next, you can use various HTML elements to format and render the query results. Here is some sample code:
<!-- HTML 表格中显示查询结果 -->
姓名 | 年龄 | " . $row["name"] . " | " . $row["age"] . " | "; } ?>
---|
With the above sample code, you can format the query results using HTML elements such as tables or cards and render them on the user interface.
It should be noted that it is necessary to iterate over the query results and output the data of each row to the user interface. You must also ensure that the query results are not empty before using them. In this example, the mysqli_num_rows function is used to detect how many rows the query result contains.
In summary, PHP can connect to a database by using mysqli or PDO extensions and query data from the database using SQL statements. Once you have the query results, you can use various HTML elements to format and render it. Finally, be sure to do the necessary checks to ensure that the query result is non-empty, and iterate over it to output the data to the user interface.
The above is the detailed content of php output showing query results. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
