Php 从查询的第一行获取字段
我正在使用Codeigniter的活动记录类。因此,查询如下所示:Php 从查询的第一行获取字段,php,codeigniter,Php,Codeigniter,我正在使用Codeigniter的活动记录类。因此,查询如下所示: $query = $this->db->get_where('Table', array('field' => $value)); 现在,从第一排获得字段的最快方法是什么? 将$query->first_row->field;工作 谢谢 虽然快速是美妙的,但错误不是!在尝试使用($query->num_rows()>0)访问结果之前,请确保始终检查结果。 $query->first_row()->
$query = $this->db->get_where('Table', array('field' => $value));
现在,从第一排获得字段的最快方法是什么?
将$query->first_row->field
;工作
谢谢 虽然快速是美妙的,但错误不是!在尝试使用
($query->num_rows()>0)访问结果之前,请确保始终检查结果。
$query->first_row()->field
最快(最简洁)的方式:
$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 = $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 = $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;
}
谢谢你的详细回答!