With the popularity and development of the Internet, the development of Web applications has become more and more common. PHP is a scripting language widely used in web development and it is very powerful in terms of integration with MySQL database. Querying the database is a basic operation of web applications, so this article will introduce you how to use PHP to query data and return an array.
In PHP, the first step in connecting to a MySQL database is to create a connection using the mysqli_connect() function. This function requires four parameters: hostname, username, password and database name. The following is sample code to establish a connection:
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database" ;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check whether the connection is successful
if (! $conn) {
die("连接失败:" . mysqli_connect_error());
}
After establishing a connection with the database, we can use the mysqli_query() function to execute the query statement . This function returns a result set object containing the rows retrieved from the database. The following are some sample query statements:
//Query all rows
$sql = "SELECT * FROM customers";
$result = mysqli_query($conn, $sql);
// Query specific rows
$sql = "SELECT * FROM customers WHERE city='New York'";
$result = mysqli_query($conn, $sql);
// Query specific columns
$sql = "SELECT name, email FROM customers";
$result = mysqli_query($conn, $sql);
Please note that these query statements are only examples, and It should be modified according to your actual needs.
In order to store the query results in an array, we need to use the mysqli_fetch_assoc() function, which retrieves a row from the result set and Convert it to an associative array. The following is a sample code that stores query results in an array:
// Stores query results in an array
$customers = array();
if (mysqli_num_rows($result) > ; 0) {
while ($row = mysqli_fetch_assoc($result)) { $customers[] = $row; }
}
//Output the results in the array
foreach ($customers as $customer) {
echo "Name: " . $customer['name'] . "<br>"; echo "Email: " . $customer['email'] . "<br>";
}
In the above example code, we first created an empty array named $customers. We then use the mysqli_num_rows() function to check if there are any rows in the result set to make sure we queried the data. Next, we use a while loop to iterate through all the rows in the result set and use the mysqli_fetch_assoc() function to convert each row into an associative array and finally add it to the $customers array. After the data is stored in the array, we use a foreach loop to output the name and email for each customer information.
After completing the query, we need to use the mysqli_close() function to close the connection to the database. The following is the sample code:
//Close the connection
mysqli_close($conn);
Summary
In this article, we introduced how to use PHP to query data and Store the results in an array. First, we establish a connection to the MySQL database through the mysqli_connect() function. Then, we use the mysqli_query() function to execute the query statement and obtain the result set object corresponding to the result. Next, we use the mysqli_fetch_assoc() function to store the results in an array. Finally, we close the database connection using the mysqli_close() function.
I hope this article was helpful to you and makes it easier for you to use PHP to query data and return arrays. If you have any questions about this, please feel free to ask in the comment section below.
The above is the detailed content of How to query data using PHP and return an array. For more information, please follow other related articles on the PHP Chinese website!