PHP is a scripting language widely used in Web development. It can interact with databases such as MySQL to implement database operations such as query, insert, update, and delete. In this article, we will take an in-depth look at how PHP query statements query results.
1. Connect to the database
Before performing database operations, you need to connect to the database first, use the following code:
$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 创建连接 $conn = mysqli_connect($servername, $username, $password, $dbname); // 检查连接是否成功 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
The mysqli_connect() function is used here to create a connection, and Check if the connection is successful. Among them, $servername represents the host name, $username represents the user name, $password represents the password, and $dbname represents the database name.
2. Query Statement
Use PHP query statement to query data from the database. The following is an example:
$sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql);
The SELECT statement is used to query the id, firstname and lastname fields from the MyGuests table. The mysqli_query() function executes the query and stores the results in the $result variable.
3. Output the query results
To output the query results, you need to use PHP's while loop:
if (mysqli_num_rows($result) > 0) { // 输出数据 while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 结果"; }
The mysqli_num_rows() function is used here to check whether the result is empty, and then Use the mysqli_fetch_assoc() function to convert the query results into an associative array. The while loop traverses the array and outputs each row of data.
4. Complete code example
The following is a complete code example:
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 结果"; } mysqli_close($conn); ?>
The above code will output the id, firstname and lastname fields queried from the MyGuests table.
5. Summary
Querying the results in the PHP query statement is very simple. You only need to use the SELECT statement and store the results in variables. Use a while loop to iterate through the results and output each row of data. Remember to connect to the database before performing database operations to ensure that the operation proceeds normally.
The above is the detailed content of Learn more about how PHP query statements query results. For more information, please follow other related articles on the PHP Chinese website!