Fatal error: Cannot use object of type CI_DB_mysql_result as array
Fatal error: Cannot use object of type CI_DB_mysql_result as array
在 post 这个问题之前,我已经阅读了 1, 2 和其他几个线程,但是其中 none 解决了我的问题。我是 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
returns 二维数组包含来自 table 的多行。在这种情况下,您只需要一行,因此您可以使用以下任一方法:
row()
方法,returns 结果作为对象。
$result = $this->db->get()->row();
然后$result->choices_count
得到计数值
row_array()
方法,returns 结果为数组。
$result = $this->db->get()->row_array();
然后$result['choices_count']
得到计数值
unbuffered_row()
方法,returns 结果作为对象
$result = $this->db->get()->unbuffered_row();
然后$result->choices_count
得到计数值
unbuffered_row('array')
方法,returns 结果为数组。
$result = $this->db->get()->unbuffered_row('array');
然后$result['choices_count']
得到计数值
在 post 这个问题之前,我已经阅读了 1, 2 和其他几个线程,但是其中 none 解决了我的问题。我是 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
returns 二维数组包含来自 table 的多行。在这种情况下,您只需要一行,因此您可以使用以下任一方法:
row()
方法,returns 结果作为对象。$result = $this->db->get()->row();
然后$result->choices_count
得到计数值row_array()
方法,returns 结果为数组。$result = $this->db->get()->row_array();
然后$result['choices_count']
得到计数值unbuffered_row()
方法,returns 结果作为对象$result = $this->db->get()->unbuffered_row();
然后$result->choices_count
得到计数值unbuffered_row('array')
方法,returns 结果为数组。$result = $this->db->get()->unbuffered_row('array');
然后$result['choices_count']
得到计数值