php - 致命错误:不能使用 CI_DB_mysql_result 类型的对象作为数组
问题描述
在发布此问题之前,我已阅读1、2和其他几个线程,但没有一个能解决我的问题。我对 PHP 完全陌生,并且一直在尝试使用 codeigniter 学习 MVC。导致错误的功能是
public function get_results()
{
$libraries = array("", "JQuery", "MooTools", "YUI Library", "Glow");
$table_rows = '';
for ($i = 1; $i < 5; $i++)
{
$this->db->select('COUNT(choice) choices_count')->from('js_libraries')->where('choice',$i);
$result = $this->db->get();
$table_rows .= "<tr><td>" . $libraries[$i] . " Got:</td><td><b>" . $result[0] . "</b> votes</td></tr>";
}
}
我已经在 phpmyadmin 上执行了查询,它显示了结果(单个整数),但我无法使这个CI_DB_mysql_result
对象工作。我已经尝试了所有result(), result_array(), getResult()
函数,但它说这种对象的未定义函数。我怎样才能得到结果CI_DB_mysql_result
?
解决方案
好的。那是因为result_array
返回包含表中多行的二维数组。在这种情况下,您只需要一行,因此您可以使用以下任一项:
row()
将结果作为对象返回的方法。$result = $this->db->get()->row();
然后$result->choices_count
得到计数值。row_array()
将结果作为数组返回的方法。$result = $this->db->get()->row_array();
然后$result['choices_count']
得到计数值。unbuffered_row()
将结果作为对象返回的方法$result = $this->db->get()->unbuffered_row();
然后$result->choices_count
获取计数值。unbuffered_row('array')
将结果作为数组返回的方法。$result = $this->db->get()->unbuffered_row('array');
然后$result['choices_count']
得到计数值。
推荐阅读
- java - 如何使用没有函数的 lambda从现有列表创建新列表
- bash - 从 curl GET 响应中读取 json 值
- arrays - 指向数组 C 中寄存器值的指针
- fipy - FiPy 2D Navier Stokes 实现:一个方向的导数问题
- ios - 如何使用 presentViewController 在 CDVPlugin 中的 Cordova 中显示 ViewController
- c# - 如何在 CheckListBox 中获取选中的项目字符串
- webrtc - 如何从 WebRTC PeerConnection 获取多个流
- c# - 等到所有文件都下载完毕,然后等到对这些文件的处理完成
- python - 在 Python 中使用 Pandas 将字符串对象转换为时间戳的最有效方法
- javafx - 带有复选框的JavaFx TreeTableView,将监听器放在哪里?