简体   繁体   中英

PDO MYSQL nested COUNT breaks my query

I have query like this:

$query = $con->prepare("SELECT `Id`, 
                                (SELECT count(`Id`) 
                                 FROM `images` 
                                 WHERE `parentId` = :recId) as `numImgs` 
                       FROM `records`
                       WHERE `id`= :recId LIMIT 1");

$query->execute(array('recId' => $recId));

$rec = $query->fetch(PDO::FETCH_ASSOC);

When I do this without the nested (SELECT Count(Id)) the query works. If I take out the SELECT(COUNT(Id)) and do it on it's own, it also works.

For some reason the above query doesn't work. I don't get any errors, just no results. However if I run the query inside phpMyAdmin, it works without any problem and returns two columns, Id and numImgs, eg:

----------------
| id | numImgs |
----------------
| 50 |   10    |
----------------

I've tested the value I'm passing, it is being correctly populated from $recId so there's no issue there. Can anyone point me in the right direction as to what's going wrong with this?

Thanks!

NOTE: this works perfectly, but I don't understand why I can't do it with one query:

try{
  $query = $con->prepare("SELECT `Id` 
                                  FROM `records`
                                  WHERE `id`= :recId
                                  AND `ownerId` = :userId
                                  LIMIT 1");
  $query->execute(array('recId' => $recId, 'userId' => $userId));
  $rec = $query->fetch(PDO::FETCH_ASSOC);
}catch(PDOException $e) {
  dump_exception('Exception selecting record.', $e);
}
if($rec){
  try{
    $picQuery = $con->prepare("SELECT COUNT(`Id`)
                                      FROM `images` 
                                      WHERE `parentId`= :recId");
    $picQuery->execute(array('recId' => $recId));
    $numPics = $picQuery->fetchColumn();
  }catch(PDOException $e) {
    dump_exception('Exception counting pictures.', $e);
  }

Can't you use JOIN s and GROUP BY ? It would look like this if I got your question right.

SELECT `id`, COUNT(*) AS `numImgs`
FROM `records` r
INNER JOIN `images` i ON i.parentId = r.id
WHERE r.id = :recId
AND r.ownerId = :userId
GROUP BY r.id
LIMIT 1

Looks like I've found the problem thanks to @RyanVincent's comment below. Whilst I have other working queries which use the same parameter more than once without it having to be passed into the "execute" array more than once, in this case, it seems that only listing the parameter once is causing the issue. I suspect that because it's a nested query, it treats it as two independent queries and as such, one has no access to the parameters of another. That's just a guess, I may be wrong, but it seems to make sense based on my results. Not only does the parameter have to be listed twice, but it must also be uniquely named otherwise you get exactly the same problem. So this code fixed the problem:

$query = $con->prepare("SELECT `Id`, 
                                (SELECT count(`Id`) 
                                 FROM `images` 
                                 WHERE `parentId` = :recIdOne) as `numImgs` 
                       FROM `records`
                       WHERE `id`= :recIdTwo LIMIT 1");

$query->execute(array('recIdOne' => $recId, 'recIdTwo' => $recId));

$rec = $query->fetch(PDO::FETCH_ASSOC);

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