Home  >  Article  >  Backend Development  >  How to fill in the query results into the form in php

How to fill in the query results into the form in php

PHPz
PHPzOriginal
2023-04-24 14:52:20955browse

In Web development, tables are one of the commonly used elements. Whether used to present data, collect information, or perform calculations, tables are an indispensable tool. As a popular server-side programming language, PHP can obtain data by querying the database and fill it in a form to display to the user. This article will introduce how to fill in the form using PHP query results.

  1. Connect to the database
    First, we need to connect to the database to perform query operations. In PHP, you can use extension libraries such as mysqli or PDO to connect to different types of databases. The following is a sample code for using mysqli to connect to a MySQL database:
//连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password, $dbname);
//检测连接是否成功
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
  1. Query data
    After connecting to the database, you can start querying data. In this article, we will use a table named "students" as an example, which contains three fields: id, name, age. The following is a sample code to query all student information:
//查询数据
$sql = "SELECT * FROM students";
$result = mysqli_query($conn, $sql);
//检测查询是否成功
if (mysqli_num_rows($result) > 0) {
    //输出每一行数据
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Age: " . $row["age"]. "<br>";
    }
} else {
    echo "0 results";
}
  1. Fill in the form
    After the query results are successfully obtained, you can fill them in the form. First, we need to create a table element in the HTML code and define the header. Here is sample code to create a table with three fields:
<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Age</th>
    </tr>
</table>

Then, we need to iterate through the query results in PHP code and fill each row of data into the table. The following is a sample code for filling the query results into the form:

//创建表格,并填入数据
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Age</th>";
echo "</tr>";
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "<tr>";
        echo "<td>" . $row["id"] . "</td>";
        echo "<td>" . $row["name"] . "</td>";
        echo "<td>" . $row["age"] . "</td>";
        echo "</tr>";
    }
} else {
    echo "0 results";
}
echo "</table>";
  1. Complete sample code
    Finally, we integrate the above three steps to get a complete example for filling the query results into the table Code:
//连接数据库
$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 * FROM students";
$result = mysqli_query($conn, $sql);
//创建表格,并填入数据
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Age</th>";
echo "</tr>";
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "<tr>";
        echo "<td>" . $row["id"] . "</td>";
        echo "<td>" . $row["name"] . "</td>";
        echo "<td>" . $row["age"] . "</td>";
        echo "</tr>";
    }
} else {
    echo "0 results";
}
echo "</table>";
//关闭连接
mysqli_close($conn);

The above sample code is just a demonstration and developers can modify it according to their own needs. For example, you can use SQL statements to filter out data that meets specific conditions, or use CSS styles to beautify the appearance of tables, etc. I hope this article can provide some help to developers who use PHP query results to fill in forms.

The above is the detailed content of How to fill in the query results into the form in 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