dongtiao2066 2015-02-13 17:07 采纳率: 0%
浏览 92
已采纳

PDO Select语句返回二维数组,如何从该数组中打印一个值

In my project i am trying to select an id field and a varchar field from a database and i want to return a 2 dimentional array containing the id fields and the varchar fields.This is what i have done:

        foreach($dbh->query("SELECT status, post_id FROM status WHERE user_id = " . $user_id) as $row_all){
            $ret_array[] = $row_all; 
        }


        return @$ret_array;

When i print the value on another page:

foreach ($all_stats as $key => $value) {
    foreach ((array)$value as $post_id => $status) {
        echo $status;

    }
}

When it is printed on the page i get:

"Second post" 1 1

I would like to print only value that is held in the status part of the array. Hope someone can help.

  • 写回答

2条回答 默认 最新

  • dsa111111 2015-02-13 17:14
    关注

    As you already use PDO, one should consider using PDO-Statements. After having prepared the query and executed the statements, it's results can be diretly received by $array = $Statement->fetchAll(PDO::FETCH_ASSOC);. That returns an associative array like the following :

    [0] => ID => 1
        => Status => 0
    
    [1] => ID => 2
        => Status => 1
    

    Then, you can loop through that array like shown here :

    for ($i = 0; $i < count($array); $i++){
        echo $array[$i]['status'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀