How can CONCAT be used in conjunction with GROUP_CONCAT to format data output in PHP?

When using GROUP_CONCAT in MySQL to concatenate multiple rows into a single string, you may also want to use CONCAT to format the data output. This can be achieved by concatenating additional strings or values within the GROUP_CONCAT function using CONCAT. This allows you to customize the formatting of the concatenated data before it is returned.

$query = "SELECT GROUP_CONCAT(CONCAT('Name: ', name, ' - Age: ', age) SEPARATOR '; ') AS formatted_data FROM users";
$result = mysqli_query($connection, $query);

if($result){
    $row = mysqli_fetch_assoc($result);
    echo $row['formatted_data'];
} else {
    echo "Error: " . mysqli_error($connection);
}