6
我正在使用Codeigniter的活动记录类。所以查询看起来是这样的:从查询的第一行获取字段
$query = $this->db->get_where('Table', array('field' => $value));
现在,从第一行获取字段的最快方法是什么? 会$query->first_row->field
;工作?
谢谢!
我正在使用Codeigniter的活动记录类。所以查询看起来是这样的:从查询的第一行获取字段
$query = $this->db->get_where('Table', array('field' => $value));
现在,从第一行获取字段的最快方法是什么? 会$query->first_row->field
;工作?
谢谢!
虽然速度很快,但错误并不是!确保你总是检查结果尝试与访问之前($query->num_rows() > 0)
最快(最简洁)的方式:
$query = $this->db->get_where('Table', array('field' => $value));
echo(($query->num_rows() > 0) ? $query->first_row()->field : 'No Results');
基本上是一样的:
$query = $this->db->get_where('Table', array('field' => $value));
if($query->num_rows() > 0)
{
echo $query->first_row()->field;
}
else
{
echo 'No Results';
}
对于多个字段使用:
$query = $this->db->get_where('Table', array('field' => $value));
if ($query->num_rows() > 0)
{
$row = $query->row();
echo $row->title;
echo $row->name;
echo $row->body;
}
$query->first_row()->field
感谢您的详细解答! – skndstry 2010-12-08 14:06:38