Home >Backend Development >PHP Problem >How to query data with PHP+MySQL and output the results as a table

How to query data with PHP+MySQL and output the results as a table

PHPz
PHPzOriginal
2023-04-11 10:43:571718browse

PHP is a popular server-side scripting language that can be used to create dynamic web pages. When using PHP, you can use the MySQL database to store data and SQL to query the data. In this article, we will see how to query data from a MySQL database using PHP and output the results as a table.

Preparation

Before we begin, we need to have a web server environment running PHP and MySQL. You can use a development environment like AMP or MAMP, or you can install it on your cloud server or local machine. In addition, we also need to have a MySQL database containing data to query and output results.

Database Query

First, we need to establish a PHP connection to the MySQL database. To connect to the database, we use the PHP built-in function mysqli_connect(). This function will return an object that will be used for subsequent database operations. The following is the code used to connect to the database:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Please replace $username, $password and $dbname in the above code with your own values.

Now, we are connected to the database and ready to execute the query. We can select data from the database using SELECT statement. For example, the following example queries all rows in a table named "users":

<?php
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
?>

Please note that we use the mysqli_query() function to send the SELECT statement to the MySQL database and return the results. The result is stored in the $result variable, which we will use later to output the result.

Output results

Next, we will use the table tag in PHP to display the query results. The following is the code to output the results:

<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Email</th>
    </tr>
    <?php while ($row = mysqli_fetch_assoc($result)) { ?>
        <tr>
            <td><?php echo $row[&#39;id&#39;]; ?></td>
            <td><?php echo $row[&#39;name&#39;]; ?></td>
            <td><?php echo $row[&#39;email&#39;]; ?></td>
        </tr>
    <?php } ?>
</table>

Please note that we use a while loop to iterate through each row of results and use the mysqli_fetch_assoc() function to obtain the associative array of each row. We then output the values ​​in each row into a table.

Full sample code

The following is the complete sample code to query a MySQL database and output the results as a table:


<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Email</th>
    </tr>
    <?php while ($row = mysqli_fetch_assoc($result)) { ?>
        <tr>
            <td><?php echo $row[&#39;id&#39;]; ?></td>
            <td><?php echo $row[&#39;name&#39;]; ?></td>
            <td><?php echo $row[&#39;email&#39;]; ?></td>
        </tr>
    <?php } ?>
</table>

Conclusion

This article explains how Use PHP to query data from a MySQL database and output the results as a table. We showed how to connect to the database, how to execute the query, and how to output the results as a table. You can now apply this knowledge to create your own PHP database queries and create dynamic content on your website.

The above is the detailed content of How to query data with PHP+MySQL and output the results as a table. 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