Home  >  Article  >  Backend Development  >  How to query MySQL using PHP and return an array

How to query MySQL using PHP and return an array

PHPz
PHPzOriginal
2023-04-12 09:18:03624browse

PHP is a commonly used programming language, especially in the field of web development, it is widely used. When writing web applications using PHP, we often need to query the database and display the query results in the web application. Therefore, the integration of PHP with the MySQL database becomes particularly important. In this post, we will discuss how to query MySQL using PHP and return an array.

Before we start, we need to prepare some things:

  • A web server with PHP and MySQL installed
  • A MySQL database containing some data
  • Know how to connect to a MySQL database

Now, assuming we have these materials ready, we can start using PHP to query MySQL and return an array.

First, we need to create a PHP script that connects to the MySQL database. Here we can use the mysqli class in PHP to establish the connection. The following is an example of establishing a connection:

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

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

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

In this example, we use four variables to represent the name of the MySQL server, user name, password and database name. We also used the mysqli class to create a new connection object and check whether the connection was successful. If the connection fails, it will display a connection error message and exit the script.

Next, we use the SELECT statement to retrieve data from the MySQL database. Below is an example of using a SELECT statement to retrieve data from a table named "customers":

<?php
$sql = "SELECT * FROM customers";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 将查询结果存储到数组中
    $data = array();
    while($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
} else {
    echo "0 结果";
}
?>

In this example, we have used a SELECT statement to retrieve all data from a table named "customers". We use query() method to execute the query and check if there are any rows in the result set. If the query returns some rows, then we store the query results in an array called $data for use in the web application.

To store the query results, we use a while loop to iterate each row in the result set and add it to the data array.

Finally, we can use the $data array in the web application to display the query results. For example, we can display the query results as an HTML table:

<table>
    <tr>
        <th>姓名</th>
        <th>地址</th>
        <th>城市</th>
        <th>电话号码</th>
    </tr>
    <?php foreach ($data as $row): ?>
        <tr>
            <td><?php echo $row[&#39;name&#39;]; ?></td>
            <td><?php echo $row[&#39;address&#39;]; ?></td>
            <td><?php echo $row[&#39;city&#39;]; ?></td>
            <td><?php echo $row[&#39;phone&#39;]; ?></td>
        </tr>
    <?php endforeach; ?>
</table>

In this example, we use a foreach loop to iterate each row in the $data array and display the data using an HTML table. Note that we access the data fields in each data row through the associative array in the $data array.

We can easily display data in a web application by using PHP to query MySQL and return an array. This makes our web application powerful and easy to maintain. Therefore, PHP querying MySQL to return an array is a very useful trick in web development.

The above is the detailed content of How to query MySQL using PHP and return an array. 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