Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从查询的第一行获取字段_Php_Codeigniter - Fatal编程技术网

Php 从查询的第一行获取字段

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()->

我正在使用Codeigniter的活动记录类。因此,查询如下所示:

$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;
}

谢谢你的详细回答!