Home > Article > Backend Development > How to store database in array in php
PHP is a very popular Web programming language. It is not only flexible, open source, and easy to learn, but also can easily interact with databases. In the process of developing web applications, it is often necessary to store large amounts of data, and storing data in arrays is a common practice. This article will introduce how PHP stores database data in arrays.
1. PHP connection to the database
In the process of storing data in the database, you first need to connect to the database. PHP provides many ways to connect to the database, such as mysqli, PDO, etc. Here we use mysqli as an example to explain.
You need to pass in 4 parameters to connect to the mysqli database: host address, user name, password, database name. After the connection is successful, a mysqli object will be returned. The code to connect to the database is as follows:
$host = 'localhost'; $user = 'root'; $password = 'password'; $database = 'database'; $mysqli = new mysqli($host, $user, $password, $database); if ($mysqli->connect_errno) { die('Connect Error: ' . $mysqli->connect_error); }
In the above code, $host is the host address, $user is the user name, $password is the password, and $database is the database name. If the connection fails, an error message will be output and the execution of the script will end.
2. Store database data in the array
After the connection is successful, the database data can be stored in the array. The following is a sample code that retrieves data from the database and stores it in an array through mysqli:
$sql = "SELECT * FROM users"; $result = $mysqli->query($sql); $users = array(); while ($row = $result->fetch_assoc()) { $users[] = $row; }
First, a $sql variable is defined to store SQL query statements, which will be retrieved from the table named users Return all row data. Then, call the query() method of the $mysqli object to execute the query and store the results in the $result variable.
Next, a $users array is defined to store data queried from the database. Iterate through the query results through a while loop and store each row of data in the $users array. The $row variable is used to store each row of data and is pushed into the array after the data is stored. Finally, the $users array obtained is the user information taken out from the database.
3. Add new data to the array
When adding new data to the array, you usually need to insert data into the table. The following is a sample code that uses mysqli to insert new data into the database and store it in an array:
$insertName = 'Mary'; $insertAge = 25; $insertEmail = 'mary@example.com'; $sql = "INSERT INTO users (name, age, email) VALUES ('$insertName', $insertAge, '$insertEmail')"; $mysqli->query($sql); $users = array(); $sql = "SELECT * FROM users"; $result = $mysqli->query($sql); while ($row = $result->fetch_assoc()) { $users[] = $row; }
First, three variables, $insertName, $insertAge, and $insertEmail, are defined to store new user data. Then, generate an $insert statement to insert new user data into the database.
Next, execute the $query statement and pass in the $insert statement through the query() method of the mysqli object. If the $insert statement is executed successfully, the addition is successful. Finally, use a method similar to the previous one to re-query all the data from the database and store it in the array $users.
4. Summary
The above is the relevant content of how PHP stores database data in an array. In actual development, PHP's array operation is a very convenient and flexible way. Arrays can be used to efficiently operate and manage large amounts of data during the development process. Through the above examples, readers can learn how to use mysqli to establish a connection with the database, use mysqli to retrieve data and store it in an array, and how to use mysqli to insert new data and update it and store it in an array.
The above is the detailed content of How to store database in array in php. For more information, please follow other related articles on the PHP Chinese website!