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

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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系