我正在使用Codeigniter的Active Record Class.所以查询看起来像这样:
$query = $this->db->get_where('Table', array('field' => $value));
Run Code Online (Sandbox Code Playgroud)
现在,从第一行获取字段的最快方法是什么?会的$query->first_row->field; 工作?
谢谢!
Nex*_*Rex 10
虽然速度很快,但错误不是!确保在尝试访问结果之前始终检查结果($query->num_rows() > 0)
最快(最简洁)的方式:
$query = $this->db->get_where('Table', array('field' => $value));
echo(($query->num_rows() > 0) ? $query->first_row()->field : 'No Results');
Run Code Online (Sandbox Code Playgroud)
基本相同:
$query = $this->db->get_where('Table', array('field' => $value));
if($query->num_rows() > 0)
{
echo $query->first_row()->field;
}
else
{
echo 'No Results';
}
Run Code Online (Sandbox Code Playgroud)
对于多个字段使用:
$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;
}
Run Code Online (Sandbox Code Playgroud)