Home Backend Development PHP Problem How to query data in php and store it in an array

How to query data in php and store it in an array

Apr 27, 2023 am 09:07 AM

Performing data query operations in PHP is a very common task. Sometimes we need to record the queried data to facilitate subsequent operations. At this time, we need to store the queried data into an array. This article will introduce to you how to store the queried data into an array in PHP.

1. Connect to the database and perform query operations

Before performing data query operations, you need to connect to the database first. Assuming that we already have the correct database connection information, use the following code to connect:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
Copy after login

Next, we can use SQL statements to perform data query operations, for example:

$sql = "SELECT id, name, age FROM users";
$result = $conn->query($sql);
Copy after login

The query results will be is stored in the $result variable. It should be noted that if the query result is empty, $result will be an empty result set object.

2. Store the query results in an array

To store the query results in an array, we can use the mysqli_fetch_array function in PHP to obtain the data in the result set row by row and store them Stored in array.

$users = array(); // 定义一个空数组

while ($row = mysqli_fetch_array($result)) {
    $users[] = array(
        'id' => $row['id'],
        'name' => $row['name'],
        'age' => $row['age']
    );
}
Copy after login

The function of the above code is to store each row of data in the query results in the array $users. Each row of data is stored in an associative array, where the key is the name of the column and the key value is the value of the column.

3. Complete code example

The following is a complete example code for connecting to the database, executing queries, and storing the results in an array:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

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

$sql = "SELECT id, name, age FROM users";
$result = $conn->query($sql);

$users = array(); // 定义一个空数组

while ($row = mysqli_fetch_array($result)) {
    $users[] = array(
        'id' => $row['id'],
        'name' => $row['name'],
        'age' => $row['age']
    );
}

// 打印结果
print_r($users);

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

4. Summary

This article introduces you to the method of storing the queried data into an array in PHP, that is, using the mysqli_fetch_array function to obtain the query results row by row and store them in the array. It should be noted that when performing database operations, you must pay attention to the issue of SQL injection vulnerabilities to ensure the security of the program.

The above is the detailed content of How to query data in php and store it in an array. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24