在发布这个问题之前,我已经阅读了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数组。在这种情况下,您只需要一行,因此可以使用以下任一项:
-
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');
然后CCD_ 16得到计数值。