Home  >  Article  >  Backend Development  >  How to query a database using PHP and store the results as a 2D array

How to query a database using PHP and store the results as a 2D array

PHPz
PHPzOriginal
2023-04-27 09:05:12741browse

In PHP, we usually use SQL statements to query data in the database, and the result is often a two-dimensional array, that is, an array containing multiple records. This article will introduce how to use PHP language to query the database and store the results as a two-dimensional array.

1. Establish a database connection

Before querying the database, you first need to establish a database connection. This can be easily accomplished using PHP's built-in mysqli or PDO extensions. The following is a sample code for establishing a connection using mysqli extension:

// 连接数据库
$host = 'localhost';
$user = 'root';
$password = '';
$dbname = 'test';
$conn = new mysqli($host, $user, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_errno) {
    die('连接失败: ' . $conn->connect_error);
}

2. Execute SQL query statement

After completing the database connection, we need to execute SQL query statement to obtain data. The query statement can be any legal SQL statement. For example, the SELECT statement can be used to obtain the value of a specific field from the database. The following is an example:

//执行查询并存储结果
$sql = "SELECT id, name, age FROM users";
$result = $conn->query($sql);
//检查结果是否为空
if ($result->num_rows > 0) {
    // 存储结果为二维数组
    $data = array();
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
}

In the above code, we use the mysqli extended query method to execute the SQL query statement. After the query is successful, we use the num_rows method to check whether the result is empty. If the result is not empty, use the fetch_assoc method to store each row of data into an associative array, and add the array to the $data array. Finally, all data is stored in the $data array. So far, we have successfully stored the query results into a two-dimensional array.

3. Use the query results

Once the query results are stored as a two-dimensional array, we can use PHP language to operate on it. For example, you can output each record by looping through the array:

// 遍历数组并输出每条记录
foreach ($data as $row) {
    echo $row['id'] . "\t" . $row['name'] . "\t" . $row['age'] . "\n";
}

In the above code, we use a foreach loop to traverse the $data array and output the value of the id, name, and age fields of each record, where " \t" represents a tab character, and "\n" represents a newline character.

4. Close the database connection

After using the database, we need to close the database connection to release resources and ensure security. The following is an example:

// 关闭连接
$conn->close();

In the above code, we use the close method of the mysqli extension to close the database connection.

Summary

Querying the database and storing the results as a two-dimensional array in PHP is very simple. You only need to establish a database connection, execute the SQL query statement, store the results as a two-dimensional array, and use the query The result is enough. If you are using other database extensions, such as the PDO extension, it is also very simple to implement similar operations.

The above is the detailed content of How to query a database using PHP and store the results as a 2D 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