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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测