I have an issue where Group By isn't working in my PHP code on Localhost, but it works when I run it as a query in terminal and mySQLworkbench. This is for an inventory function on a website.
This is the line of code in question:
SELECT i.id, i.itemID, ii.name, ii.category, ii.rarity, ii.image
FROM inventory AS i
LEFT JOIN items AS ii ON i.itemID = ii.itemID
WHERE userID = 1
GROUP BY i.itemID ORDER BY i.id DESC LIMIT 1, 5
What I've tried to do is run it in PHP the way we run queries with multiple results and output each line (with foreach).
The query runs fine in the PHP if I take the GROUP BY out, but unfortunately, I need the GROUP BY and I'll need it again in the future for other features.
I'm running mysql 5.7.21 Homebrew if that matters and PHP 5.6 (the site is a bit old).
The query runs on the site's production and dev servers.
edit Forgot to mention, I've tried running this code - didn't solve the issue:
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
edit This is the PHP code:
$items = database::retrieveAllRows( database::query("
SELECT i.id, i.itemID, ii.name, ii.category, ii.rarity, ii.image
FROM inventory AS i LEFT JOIN items AS ii ON i.itemID = ii.itemID WHERE userID = %u
GROUP BY i.itemID ORDER BY i.id DESC LIMIT $limit, $pageLimit", $id) );
retrieveAllRows is one of our functions:
public function retrieveAllRows ( array $res )
{
if ( $this -> validateResultSet ( $res ) === FALSE )
{
return ( NULL );
}
return ( $res [ 0 ] -> fetchAll () );
}