从 CodeIgniter 中查询的第一行获取单个字段值

发布于 2024-10-06 00:45:45 字数 265 浏览 8 评论 0原文

我正在使用 CodeIgniter 的活动记录类,查询如下所示:

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

What's the fast way to get a field value from the 第一行?

$query->first_row->field; 有效吗?

I'm using CodeIgniter's active record class and the query looks something like this:

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

What's the fastest way to get a field value from the first row?

Would $query->first_row->field; work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羞稚 2024-10-13 00:45:45

虽然速度快固然很棒,但错误却不是!确保在尝试使用 ($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;
}

Though fast is wonderful, errors aren't! Make sure you always check for results before trying to access them with ($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;
}
﹏雨一样淡蓝的深情 2024-10-13 00:45:45
$query->first_row()->field
$query->first_row()->field
酒几许 2024-10-13 00:45:45

如果您对 ->row() 返回的值进行 null 合并,则无需检查 num_rows()row() 返回一个对象或null

return $this->db
    ->get_where('Table', ['field' => $value])
    ->row()
    ->field ?? null;

或者,更简洁地使用空安全属性访问。

return $this->db
    ->get_where('Table', ['field' => $value])
    ->row()
    ?->field;

如果没有行或者第一行中的field值为null,以上两者都会返回null。如果 row() 生成的对象中没有 field 属性,故障安全脚本将发出警告。

You won't need to check num_rows() if you null coaleace the returned value from ->row(). row() return an object or null.

return $this->db
    ->get_where('Table', ['field' => $value])
    ->row()
    ->field ?? null;

Or, more concisely use null-safe property access.

return $this->db
    ->get_where('Table', ['field' => $value])
    ->row()
    ?->field;

Both of the above will return null if there are no rows or the field value in the first row is null. The fail-safe script will emit a Warning if there is no field property in the object generated by row().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文