从查询的第一行获取字段

时间:2022-06-01 22:07:17

I'm using Codeigniter's Active Record Class. So the query looks something like this:

我正在使用Codeigniter的Active Record Class。所以查询看起来像这样:

$query = $this->db->get_where('Table', array('field' => $value));

Now, what's the fastest way to get a field from the first row? Would $query->first_row->field; work?

现在,从第一行获得一个字段的最快方法是什么?将$ query-> first_row->字段;工作?

Thanks!

谢谢!

2 个解决方案

#1


9  

Though fast is wonderful, errors aren't! Make sure you always check for results before trying to access them with ($query->num_rows() > 0)

虽然速度很快,但错误不是!确保在尝试使用($ query-> num_rows()> 0)访问它们之前始终检查结果

Fastest (most concise) way:

最快(最简洁)的方式:

$query = $this->db->get_where('Table', array('field' => $value));

echo(($query->num_rows() > 0) ? $query->first_row()->field : 'No Results');

Essentially the same as:

基本相同:

$query = $this->db->get_where('Table', array('field' => $value));
if($query->num_rows() > 0)
{
    echo $query->first_row()->field;
}
else
{
    echo 'No Results';
}

For multiple fields use:

对于多个字段使用:

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

#2


8  

$query->first_row()->field

#1


9  

Though fast is wonderful, errors aren't! Make sure you always check for results before trying to access them with ($query->num_rows() > 0)

虽然速度很快,但错误不是!确保在尝试使用($ query-> num_rows()> 0)访问它们之前始终检查结果

Fastest (most concise) way:

最快(最简洁)的方式:

$query = $this->db->get_where('Table', array('field' => $value));

echo(($query->num_rows() > 0) ? $query->first_row()->field : 'No Results');

Essentially the same as:

基本相同:

$query = $this->db->get_where('Table', array('field' => $value));
if($query->num_rows() > 0)
{
    echo $query->first_row()->field;
}
else
{
    echo 'No Results';
}

For multiple fields use:

对于多个字段使用:

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

#2


8  

$query->first_row()->field