douyabu1528
2016-11-22 23:45
浏览 70
已采纳

pdo数组如何从数据库中获取所有信息

I need to fetch all the info from a query , so I can use it for other purposes, the issue is that I'm just getting 1 result of each kind and I need them all, here is my script:

<?php
  require_once 'init.php';
  $base_datos = DB::getInstance();

  $base_datos->query ("SELECT lname, fname, category,user_id, SUM(points) as Point, SUM(amount)as Amount FROM request GROUP BY user_id");
   $get_info = $base_datos->results();
   $real_info = $get_info[0];
   $name = $real_info->lname;
   $last_name = $real_info->fname;
   $categories = $real_info->category;
   echo "$name";
   ///var_dump ($get_info);
  ?>

when echo $name all i get is one name i need to get them all and so on with the other value, when i do var_dump i got all i need, but i need to loop through,I'd searched all around and was not lucky. enter image description here

this is what I'm trying to output on php page

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxin9135 2016-11-22 23:50
    已采纳

    I'm assuming $get_info is an array of objects based on how your code is currently written. Right now you're setting the first record of $get_info to $real_info.

    So to access all of the records loop through $get_info like this.

    foreach($get_info as $real_info) {
        echo $real_info->fname.' '.$real_info->lname;
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题