Current Location: Home> Latest Articles> Use implode() to connect a column in the query result

Use implode() to connect a column in the query result

M66 2025-05-17

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.

Step 1: Connect to the database

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);
}
?>

Step 2: Execute SQL Query

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();
?>

Step 3: Parsing the code

  1. $conn->query($sql) : This line of code executes a query and saves the query results in the $result variable.

  2. $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.

  3. 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.

Things to note

  • 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.

Complete code example

 <?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();
?>

Summarize

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.