有人能帮我如何将这个php查询转换为Codeigniter标准吗?while语句可以在CI中使用吗?
这是我已经尝试转换为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]);
}
我知道这个问题有一些重复,我已经尝试过了,但对我不起作用
型号
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];
}