Home  >  Article  >  Backend Development  >  How to query in php that the record is an array

How to query in php that the record is an array

PHPz
PHPzOriginal
2023-04-26 10:27:12435browse

In PHP development, querying the database is very common. We often need to obtain some data through database query, and then process or display the data. At this time, we usually save the query results in an array and traverse the array in order to display or use the data.

Next let's take a look at how to save the query results as an array in PHP and operate on the array.

Connect to the database

First, we need to connect to the database. In PHP, we can use extensions like mysqli or PDO to connect to the database. Here we take mysqli as an example to show how to connect to the database and obtain query results.

//连接MySQL数据库
$mysqli = new mysqli('localhost', 'root', 'password', 'database_name');

//判断连接是否成功
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    exit();
}

$mysqli here is the database object we are connected to, and then we can perform query operations.

Query the database

The steps to query the database using mysqli are as follows:

1. Write the SQL statement

$sql = 'SELECT * FROM table_name WHERE ...';

$sql here is the SQL we want to execute statement, table_name is the name of the table to be queried, and the condition after WHERE is optional and can be added as needed.

2. Execute the SQL statement

$result = $mysqli->query($sql);

Use the mysqli->query() method to execute the SQL statement and save the result in $result.

3. Get the query results from $result

$rows = array();

while ($row = $result->fetch_assoc()) {
    $rows[] = $row;
}

A while loop is used here to obtain the query results by calling the $result->fetch_assoc() method, and save each row in $ in the rows array.

At this point, we have saved the query results as an array. Let us take a look at how to operate this array.

Operation array

It is very convenient to save the query results in an array. We can use the array functions provided by PHP to implement various operations.

1. Traverse the array

foreach ($rows as $row) {
    ...
}

Here, a foreach loop is used to traverse each element $row in the array $rows.

2. Filter the array

Use the array_filter() function provided by PHP to filter the array to get the desired results.

$filtered_rows = array_filter($rows, function($row) {
    return $row['age'] > 18;
});

The above code filters out all elements in the array $rows that are older than 18 years old, and saves the results in the array $filtered_rows.

3. Sort array

Use the array_sort() function provided by PHP to sort the array to get the desired result.

usort($rows, function($a, $b) {
    return $a['age'] - $b['age'];
});

The above code will sort in ascending order according to the age field of each element in the array $rows.

4. Change the array

We can directly change the value of the array and use the array_map() function provided by PHP to make batch changes.

$rows = array_map(function($row) {
    $row['name'] = strtoupper($row['name']);
    return $row;
}, $rows);

The above code converts each name field in the $rows array to uppercase letters and returns the modified $row array.

Summary

By saving the query results in an array, we can easily operate on the database data to meet various needs. When using arrays, care should be taken not to destroy the structure of the original data to avoid data errors. In addition, you can also use other PHP functions and libraries in combination to implement complex operations and data processing.

The above is the detailed content of How to query in php that the record is 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