简体   繁体   中英

Query with GROUP_CONCAT not working with PHP

I am trying to display certain values through GROUP_CONCAT, and GROUP BY, yet only the groups are showing when I test it out, and the other values don't.

function events_calendar() {
     global $connection;
     mysqli_select_db($connection);
     $query = ("SELECT month, GROUP_CONCAT(start_date) as data FROM events GROUP BY month");
     $result = $connection->query($query);
     $str = "";
     while ($row = $result->fetch_assoc()) {
         echo $row['month'];
         echo $row['start_date'];
     }
     return $str;
}

The month values show up, but the start_date values don't.

The problem is the PHP. Try replacing:

     echo $row['start_date'];

with:

     echo $row['data'];

You need to use the column alias that you assigned in the SELECT .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM