CodeIgniter 的 get() 活动记录方法的用途是什么?
我在我的应用程序中使用 CodeIgniter,但我有点困惑。我写了一些这样的查询:
public function checkemail($email) {
$this->db->select('email')->from('user')->where('email', $email);
}
但是在CodeIgniter活动记录手册中,他们谈论$this->db->get()
。
我应该在 $this->db->select()
查询之后添加它吗?
我的函数不会产生任何错误。
我什么时候应该使用get()
?
I am using CodeIgniter for my application and I am a bit confused. I wrote some queries like this:
public function checkemail($email) {
$this->db->select('email')->from('user')->where('email', $email);
}
But in the CodeIgniter active record manual, they talk about $this->db->get()
.
Should I add it after the $this->db->select()
query?
My function doesn't produce any errors.
When should I use get()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您需要在其他方法之后运行
get()
。select()
、from()
和where()
将各自的语句添加到查询中,而 get() 实际运行查询并返回结果作为对象。在这种情况下,您可以将其添加到链的末尾。
如果您想稍后使用结果,请确保将其分配给变量。
Yes, you'll need to run
get()
after the other methods.select()
,from()
andwhere()
add their respective statements to the query, and get() actually runs the query and returns the result as an object.In this case, you could just add it on to the end of the chain.
If you want to work with the result afterwards, make sure that you are assigning it to a variable.
如果您使用
get("table_name")
,则不需要使用from("table_name")
。这似乎只是一种替代语法。从用户指南中,一直在底部显示:如图所示之前,查询的 FROM 部分可以在 $this->db->get() 函数中指定,因此请使用您喜欢的任何方法。
If you use
get("table_name")
then you don't need to usefrom("table_name")
. It's just an alternative syntax it seems.From the user guide, all the way at the bottom it says: As shown earlier, the FROM portion of your query can be specified in the $this->db->get() function, so use whichever method you prefer.
当前的脚本是无用的,因为即使您使用
get()
执行构建的查询,您的方法也不会将找到的email
值返回给调用脚本。在我看来,您的脚本意图很可能是检查用户表中是否存在传入的电子邮件字符串。如果这是真的,那么我建议返回一个布尔类型值。
如果由于某种奇怪的原因,您实际上希望返回传入的字符串,那么您需要返回结果行对象的
email
属性。如果没有名为email
的行或属性,则返回null
。在下面的脚本中,组合了
from()
方法、where()
方法和(查询执行)get()
方法如get_where()
。row()
用于指示结果数据应该是 1 一维对象或null
。The current scripting is useless because even if you execute the built query with
get()
, your method isn't returning the foundemail
value to the calling script.It seems most likely to me that your scripting intention is to check if the passed-in email string exists in the user table. If this is true, then I recommend returning a boolean type value.
If, for some odd reason, you actually wish to return the passed-in string, then you'll need to return the
email
property of the result row object. If there is no row or no property namedemail
, returnnull
.In the below script, the
from()
method, thewhere()
method, and the (query executing)get()
method are combined asget_where()
.row()
is used to dictate that the result data should be either a 1 one dimensional object ornull
.