首页 > 解决方案 > 而codeigniter中的mysqli_fetch_array

问题描述

有人可以帮我如何将此 php 查询转换为 Codeigniter 标准吗?可以在 CI 中使用 while 语句吗?

这是我已经尝试转换为 CI 的代码:

模型

function getStudents(){
    $this->db->select('id, name');
    $this->db->get('students');
}

控制器

$id = array();
$name = array();

$query = $this->md_students->getStudents();

while ($result = $query->result_array()) {
       array_push($id, $result[0]);
       array_push($name, $result[1]);
}

这是原始代码:

$id = array();
$name = array();

$query = mysqli_query($db, 'SELECT student_id, student_name FROM students');

    while ($result = mysqli_fetch_array($query)) {
           array_push($id, $result[0]);
           array_push($name, $result[1]);
    }

我知道这个问题有一些重复,我已经尝试过这些解决方案,但这对我不起作用。

标签: phpcodeigniter

解决方案


模型

    function getStudents(){
      $sql = 'SELECT student_id, student_name FROM students';
      $qry = $this->db->query($sql);
      return $qry->result_array(); //It will return an array of result
    }

控制器

    $id = array(); //array initialization
    $name = array();

    $student_list = $this->md_students->getStudents(); //call a function in model

    foreach ($student_list as $student) {
      array_push($id, $student['student_id'];
      array_push($name, $student['student_name];
    }

推荐阅读