In PHP, if we use a MySQL database for query operations, we usually use the mysqli extension to perform SQL queries. After executing the query, we may need to merge all the results of a certain column into a string for subsequent processing. At this time, the mysqli_result and implode() functions are very useful tools.
This article will show how to get query results through mysqli_result and use the implode() function to connect a column of data in the query results.
First, we need to connect to the MySQL database. Here, assume that you have configured the MySQL database on the server.
<?php
$servername = "localhost"; // Database server address
$username = "root"; // Database username
$password = ""; // Database Password
$dbname = "test_db"; // Database name used
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check if the connection is successful
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Next, we need to execute the SQL query and get the results. Let's assume we have a table called users , and there is a column in the table that is email , and we want to splice all users' email addresses into a string.
<?php
// Execute a query
$sql = "SELECT email FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Create an empty array to save the query results email
$emails = array();
// Traversal query results,Put each email Save to array
while ($row = $result->fetch_assoc()) {
$emails[] = $row['email'];
}
// use implode() Functions will all the array email Concatenate into a string
$emailList = implode(", ", $emails);
echo "Email address of all users: " . $emailList;
} else {
echo "No data was found";
}
$conn->close();
?>
$conn->query($sql) : This line of code executes a query and saves the query results in the $result variable.
$result->fetch_assoc() : This line of code gets each line of data from the query result. We add the data of the email column to an array $emails by looping.
implode(", ", $emails) : The implode() function concatenates all elements in the array (i.e. the user's email address) into a string with commas and spaces.
If there is no data in the query result, $result->num_rows will return 0 , and we need to make appropriate judgments.
When using mysqli_result to get query results, make sure that the results returned by the SQL query are valid.
<?php
$servername = "localhost"; // Database server address
$username = "root"; // Database username
$password = ""; // Database Password
$dbname = "test_db"; // Database name used
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check if the connection is successful
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Execute a query
$sql = "SELECT email FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Create an empty array to save the query results email
$emails = array();
// Traversal query results,Put each email Save to array
while ($row = $result->fetch_assoc()) {
$emails[] = $row['email'];
}
// use implode() Functions will all the array email Concatenate into a string
$emailList = implode(", ", $emails);
echo "Email address of all users: " . $emailList;
} else {
echo "No data was found";
}
$conn->close();
?>
By combining the mysqli_result and implode() functions, we can easily get a column of data in the query result and concatenate it into a string. Such methods are very useful when processing large amounts of data, especially when you need to pass the query results to other systems, or perform further data processing.