致命错误:无法将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返回包含表中多行的2D数组。在这种情况下,您只需要一行,因此可以使用以下任一项:

  1. row()方法,该方法将结果作为对象返回。$result = $this->db->get()->row();然后$result->choices_count得到计数值。

  2. row_array()方法,该方法将结果作为数组返回。$result = $this->db->get()->row_array();然后$result['choices_count']得到计数值。

  3. 返回结果作为对象的unbuffered_row()方法$result = $this->db->get()->unbuffered_row();然后$result->choices_count得到计数值。

  4. 以数组形式返回结果的unbuffered_row('array')方法。$result = $this->db->get()->unbuffered_row('array');然后CCD_ 16得到计数值。

最新更新