duanbin4847 2013-03-21 13:55
浏览 24
已采纳

使用PHP检索多行数据

I'm trying to create a function that runs a query that returns all of the data located in my MySQL database.

My current code only returns the one row of data (there are 7)

function staff_get() {
    $this->load->database();
    $sql = 'SELECT * from Staff';
    $query = $this->db->query($sql);
    $data = $query->row();
    $this->response($data, 200);
}

I'd imagine it has something to do with the line "$data = $query->row();" however I've tried switching "row" with "array" but this doesn't work. The text is designed to come out as plaintext so that I can manipulate it using a jQuery template.

Thank you for your help in advance.

  • 写回答

1条回答 默认 最新

  • donglv9813 2013-03-21 14:04
    关注

    You need to encase the results in a while loop. Something along the lines of this.

    function staff_get() {
        $this->load->database();
        $sql = 'SELECT * from Staff';
        $query = $this->db->query($sql);
        while($data = $query->row()) {
            $this->response($data, 200);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错