php - 而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]);
}
我知道这个问题有一些重复,我已经尝试过这些解决方案,但这对我不起作用。
解决方案
模型
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];
}
推荐阅读
- python - Kivy + Python + Raspberry - 如何实现倒计时
- sql - 使用连接创建视图
- java - Java Rest web 服务中缺少子对象
- python-3.x - 尝试从 python 列表中附加并打印已删除的项目
- oracle - 我在这个过程中做错了什么
- android - android - 无法获取gps坐标
- sorting - 如何使用 sort 命令根据 unix 中的最后一列对文件进行排序?
- reactjs - API 请求返回页面标记
- javascript - 通过使用 JS 单击另一个按钮类型输入将值从输入复制到剪贴板
- kubernetes - Kubernetes健康检查工作进入谷歌云平台