I have the following table in my database:
id | reference | secondID
---------------------------------------------
1 | 11 | 1
2 | 12 | 1
3 | 12 | 2
4 | 13 | 1
5 | 13 | 2
6 | 13 | 3
And I want to get the max secondID for each reference, so I'm doing the next SQL query:
SELECT *, MAX(secondID) FROM TABLE_A WHERE 1 GROUP BY REFERENCE
If i do this query directly into database through phpMyAdmin I get what I expected:
id | reference | secondID
---------------------------------------------
1 | 11 | 1
3 | 12 | 2
6 | 13 | 3
The problem is that I don't get the same result when doing this in PHP since I only get one row returned insted of three
(here's some code for the query)
$sql = "SELECT *, MAX(secondID) FROM TABLE_A WHERE 1 GROUP BY REFERENCE";
$this->result = $this->db->prepare($sql);
$confirmation = $this->result->execute();
if ($this->result->errorCode() != 0)
{
$errors = $this->result->errorInfo();
print_r($errors);
}
if ($confirmation)
{
return $this->result;
}
return $confirmation;
(and here how I get the data)
$row_number = 0;
while ($data = $db_resource->fetch(PDO::FETCH_ASSOC))
{
echo $row_number . '<br />';
$row_number++;
}
I know that if I get rid of 'MAX' and 'GROUP BY' I get more than one row in PHP but I don't understand why this is happening with those statements.
Any advice I could take?
SOLUTION
After some code revision on my part I found that while the SQL query I print to debug code is correct and works as expected it doesn't occur in PHP due to a bad assign order on bindValue() instructions that for some reason gets unnoticed.
I haven't put that code in the question for clarity sake and because is a heavy chunk of code, so sorry for losing everyones time on that question and thanks to responses.