I am having a very weird issue. I am using GROUP_CONCAT in a PHP SQL query. It works as intended, with the default comma separation between the items. However, I want a comma and space. I change the SQL query to the following:
GROUP_CONCAT(platforms.platformname SEPARATOR ', ')
The result is that my comma separated items just cease to show up. I have tried a number of different separators, but no dice. Anything other than the field name stops that bit of my page from displaying.
Any help is greatly appreciated!
Edit: apologies -- I typed the question out on an iPad and for some reason it used the wrong type. I used the correct ' in the actual code. Anyway, for some reason this got closed for being a dupe (even though I couldn't find a dupe here). Thanks to all who answered.
Edit2: Here is the $SQL string I have currently:
$sql = "select g.*, group_concat(distinct p.platformname separator ', ')
from games g
inner join platformrelations pr
on g.gameid=pr.gameid
inner join platforms p
on p.platformid=pr.platformid";
That above returns blank fields if I try to show "platforms" on my page. If I remove "distinct" and the "separator" bit, it works (i,e. it shows "Xbox One,PC"). Worth noting that I ran the query in SQL proper and it worked. So I'm not entirely sure where I'm tripping up.
Edit 3: more code
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT *, GROUP_CONCAT(platforms.platformname separator ', ') FROM games
INNER JOIN platformrelations on platformrelations.gameid = games.gameid
LEFT JOIN platforms on platformrelations.platformid = platforms.platformid";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row['GROUP_CONCAT(platforms.platformname)'];
}
} else {
echo "0 results";
}
$conn->close();
?>