从 CodeIgniter 中查询的第一行获取单个字段值
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然速度快固然很棒,但错误却不是!确保在尝试使用
($query->num_rows() > 0)
访问结果之前始终检查结果最快(最简洁)的方式:
本质上相同:
对于多个字段使用:
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:
Essentially the same as:
For multiple fields use:
如果您对
->row()
返回的值进行 null 合并,则无需检查num_rows()
。row()
返回一个对象或null
。或者,更简洁地使用空安全属性访问。
如果没有行或者第一行中的
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 ornull
.Or, more concisely use null-safe property access.
Both of the above will return
null
if there are no rows or thefield
value in the first row isnull
. The fail-safe script will emit a Warning if there is nofield
property in the object generated byrow()
.