Reputation: 888
I have a query with a sub query to get the ranks of each row based on tune_value. If I try to fetch the results I will get a general error from the PDOStatement::fetchAll();. The exectute of the prepare statement doesn't seem to throw any errors.
I have the following query
SET @rank := 0;
SELECT * FROM
(
SELECT *,
(UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(tunes.creation_date)) as age,
@rank := @rank + 1 as rank
FROM tunes
ORDER BY tune_value DESC
) as t
LEFT JOIN artists ON artists.id = t.artists_id
ORDER BY age
I prepare the statement using the following simplified php code.
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $pdo->prepare($sql);
$stmt->execute();
$stmt->fetchAll(\PDO::FETCH_ASSOC);
Is it impossible to do this subquery with php? I was thinking about maybe making a view of the subquery.
EDIT: To clarify the query works fine in phpmyadmin
Upvotes: 1
Views: 1576
Reputation: 157839
PDO doesn't support multiple queries in one call.
(Means PDO supports subqueries all right, it's just doesn't support multiple queries separated by a semicolon)
You have to call them one by one.
And to get errors from PDO, set this attribute
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
EDIT: This means the SET @rank := 0;
is a separate query and should be queried in a separate call
Upvotes: 2