Home  >  Article  >  Backend Development  >  How to query and store data into an array using PHP

How to query and store data into an array using PHP

PHPz
PHPzOriginal
2023-04-19 10:06:54665browse

In PHP, we can get the required data from the database by using query statements, and then store them in arrays for use in subsequent code. This article will introduce how to use PHP to query and store data into an array.

Preparation

Before we begin, we need to make sure that PHP and a database (such as MySQL) have been installed and configured. Next, we need to use the PHP Database Extension (PDO) to connect to the database.

In PHP, we can use the PDO class to create a connection, which provides a setAttribute() method to set the connection attributes.

The following is a sample code for using PDO to create a MySQL database connection:

// 数据库配置信息
$databaseHost = 'localhost';
$databaseName = 'testdb';
$databaseUsername = 'username';
$databasePassword = 'password';

// 连接数据库
$pdo = new PDO("mysql:host=$databaseHost;dbname=$databaseName", $databaseUsername, $databasePassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Query data

Once we have created a database connection, we can then write a query statement to obtain required data.

For example, we can query all the data in a certain table:

// 查询表中的所有数据
$query = "SELECT * FROM table_name";
$stmt = $pdo->prepare($query);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

Here, we pass the SELECT statement as a string to the prepare() method, then execute the statement and return the result Stored in the $results variable.

Please note that we use PDO's fetchAll() method to get all the data and store them as an associative array. We can also use the PDO::FETCH_NUM parameter to store them as a numerically indexed array.

We can also use the WHERE statement to filter data:

// 过滤数据
$query = "SELECT * FROM table_name WHERE column_name = :value";
$stmt = $pdo->prepare($query);
$stmt->bindValue(':value', 'filter_value');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

Here, we use the bindValue() method to bind the filter value to the placeholder in the query. When we execute the query, this placeholder will be replaced with the value we provide. We can use different symbols (such as ? or %s) to represent placeholders, depending on the query statement and the PDO driver.

Storing data into an array

We have queried the database and obtained the required data, now we need to store them as an array. In PHP, we can use a foreach loop to iterate through the query results and store them into an array.

Here is a sample code that stores query results as an array:

// 存储结果到数组中
$data = array();
foreach ($results as $row) {
    $data[] = $row;
}

Here, we use a foreach loop to iterate through the query results and store each row into the $data array. We can use the data in the $data array in subsequent code. For example, we can use the following code to output all the values ​​in the array:

// 输出数组中的值
foreach ($data as $row) {
    echo $row['column_name'];
}

Here, we use a foreach loop to iterate over the $data array and output the values ​​of the specified columns in the rows in each iteration.

Conclusion

In this article, we showed how to query data and store them into arrays using PHP and PDO. This is a very common task as it allows us to easily manipulate data in the database in code. This skill is very useful if you are developing web applications using PHP.

The above is the detailed content of How to query and store data into an array using 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