简体   繁体   English

如何从PHP中的MySQL案例查询中检索结果集

[英]How to retrieve result set from MySQL case query in PHP

I've been looking for an answer to this all afternoon so far and cannot come up with anything yet. 到目前为止,我一直在寻找这个下午的答案,目前还无法解决。 I have the following MySQL PDO query: 我有以下MySQL PDO查询:

$q = "select recip_id,

sum(case when msg_read = '0' AND msg_deleted = '0' then 1 else 0 end) uu,
sum(case when msg_read = '0' AND msg_deleted = '1' then 1 else 0 end) ud,
sum(case when msg_read = '1' AND msg_deleted = '0' then 1 else 0 end) ru,
sum(case when msg_read = '1' AND msg_deleted = '1' then 1 else 0 end) rd,
count(*) as total

from messages where recip_id = :d GROUP BY recip_id WITH ROLLUP";`

which, when I use recip_id = 18 for an example in PHPMyAdmin gives me the following table: 当我在PHPMyAdmin中使用recip_id = 18作为示例时,它给出了下表:

mysql结果表

I have tried several ways to fetch the resulting row in php so that I can use the values for another task, to no avail. 我尝试了几种方法来获取php中的结果行,以便我可以将值用于其他任务,但无济于事。 I've tried this: 我已经试过了:

$stmt = $dbo->prepare($q);
$row = $stmt->execute(array(":id" => $id));  
$total = $row['total'];
$uu = $row['uu'];
$ud = $row['ud']; 
$ru = $row['ru'];
$rd = $row['rd'];
echo "Recipient id: $id, Total: $total, UU: $uu, UD: $ud, RU: $ru, RD: $rd";

And this: 和这个:

$stmt = $dbo->prepare($q);
$stmt->bindParam(":id", $id);
$msgcount = array();
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {   
        $total = $row->total;
    $uu = $row->uu;
    $ud = $row->ud; 
    $ru = $row->ru;
    $rd = $row->rd;
     }
    echo "Recipient id: $id, Total: $total, UU: $uu, UD: $ud, RU: $ru, RD: $rd";

and this too: 这也是:

    $msgcount = array();
    $stmt = $dbo->prepare($q);
    $stmt->bindParam(":id", $id);
    while($row = $stmt->fetch(PDO::FETCH_OBJ)) {   
    array_push($msgcount, array($row['uu'], $row['uu'], $row['ud'], $row['ru'], $row['rd'])); 
}
    echo $msgcount[];

I cannot retrieve the values using my PHP script from the MySQL result set. 我无法从MySQL结果集中使用PHP脚本检索值。 I've tried serialize() on the rows and whole result set, I've tried to use PDO::FETCH_ASSOC and also unspecified fetch() and fetchAll(). 我试过对行和整个结果集进行serialize(),我试过使用PDO :: FETCH_ASSOC以及未指定的fetch()和fetchAll()。 *'ve used different combos and just get an empty result set or *I can't find an answer anywhere either. *使用了不同的连击,并且得到的结果集为空,或者*我也找不到任何答案。 Can anyone help me with this please? 有人可以帮我吗?

In your first attempt you forget to $stmt->fetch , and in the last two you forget $stmt->execute . 在第一次尝试中,您忘记了$stmt->fetch ,而在最后两次尝试中,您忘记了$stmt->execute Try this: 尝试这个:

$stmt = $dbo->prepare($q);
if ( ! $stmt->execute(array(":id" => $id)) ) die("error");  // returns true or false
$row = $stmt->fetch();                                      // add this line!
$total = $row['total'];
$uu = $row['uu'];
$ud = $row['ud']; 
$ru = $row['ru'];
$rd = $row['rd'];
echo "Recipient id: $id, Total: $total, UU: $uu, UD: $ud, RU: $ru, RD: $rd";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM