codeigniter 中的 $this->db->query() 和 $this->db->select() 有什么区别

发布于 2024-12-18 00:11:26 字数 245 浏览 3 评论 0原文

在 CodeIgniter 中,在 $this->db->query()$this->db->select() 中,我们可以选择以下行:桌子。谁能简单总结一下区别?

我们可以使用 $this->db->query() 运行任何查询。那$this->db->select()有什么用呢?为什么我们需要它?

In CodeIgniter, in both $this->db->query() and $this->db->select() we can select the rows from the table. can anyone summarize the difference briefly?

We can run any queries by using $this->db->query(). Then whats the use of $this->db->select()?? why we need it??

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

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

发布评论

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

评论(4

伏妖词 2024-12-25 00:11:26

在根本不了解 CodeIgniter 的情况下,这个名字就暴露了一切。使用 $this->db->select() 您可以构建并执行 SELECT 查询。如果有类似的方法可用,例如 $this->db->update()$this->db->delete() ,那就有意义了。

$this->db->query 看起来适合自定义查询(当可用时,您不应该这样做)

编辑:

查看文档中,现在很明显,方法 $this->db->select 是查询构建和链接的一个组成部分,您可以使用巧妙的系统来选择数据,而无需了解任何 SQL,您实际上必须在其中转义并构建您的使用 $this->db->query 进行查询

Without knowing CodeIgniter at all, the name gives it all away. With $this->db->select() you build and perform SELECT queries. It would make sense if similiar methods are available, like $this->db->update() and $this->db->delete().

$this->db->query looks like it is fit for a custom made query (which you shouldn't, when this is available)

EDIT:

Looking at the docs, it is now clear that the method $this->db->select is a integral part of query-building and chaining, where you can use a clever system to select your data without knowing any SQL, where you actually have to escape and build your query with using $this->db->query

德意的啸 2024-12-25 00:11:26

$this->db->select() 只是构建查询的一部分;事实上,它属于 Active Record 类,单独使用它没有意义,因为它只是创建 "SELECT field,field1,field3" 部分,没有其他内容。
您需要所有其他部分来构建完整的查询,并调用 ->get() 方法,然后检索结果。

$this->db->query() 只是一个函数帮助器,可让您运行作为参数传递的查询。

重要区别:Active Record 会自动转义查询,而 $this->db->query() 不会,因此您应该调用 $this->db->e​​scape () 在各个变量上或使用占位符运行它:

$sql = "INSERT INTO table (field1,field2) VALUES (?,?)";
$query = $this->db->query($sql,array('asd','lol'));

$this->db->select() just builds a part of the query; it belongs the the Active Record class, in fact, and using it alone doesn't make sense because it just creates the "SELECT field,field1,field3" part, nothing else.
You need all the other parts to build a full query, and the call the ->get() method and, on that, retrieve the results.

$this->db->query() is just a function helper that make you run the query you pass it as an argument.

Important difference: Active Record automatically escapes queries, while $this->db->query() doesn't, so you should call $this->db->escape() on individual variables or run it using placeholders:

$sql = "INSERT INTO table (field1,field2) VALUES (?,?)";
$query = $this->db->query($sql,array('asd','lol'));
念三年u 2024-12-25 00:11:26

使用 $this->db->select() 可以让您轻松编写查询。您始终可以手动编写查询并使用 $this->db->query() 执行它,但这将是一项繁琐的任务。因此,您可以使用 Active Record 类来为您带来神奇的效果。另外,它还会为你进行转义。
当您使用 Active Record 时,当您告诉 CodeIgniter 使用 $this 之类的方式获取数据时,CodeIgniter 最终将使用 $this->db->query() 执行查询->db->get()

请参阅本页的前两段,了解使用 Active Record 的其他一些好处
http://codeigniter.com/user_guide/database/active_record.html

Using the $this->db->select() offers you simplicity in writing queries. You can always write the query manually and execute it using $this->db->query(), but it will be a tedious task. So instead, you can use the Active Record class that will do the magic for you. Plus, it will also do the escaping for you.
When you're using Active Record, CodeIgniter will eventually execute the query using $this->db->query() when you tell it to get the data using something like $this->db->get().

See the first two paragraphs from this page for some other benefits of using Active Record
http://codeigniter.com/user_guide/database/active_record.html

浮华 2024-12-25 00:11:26

我认为

$this->db->query('YOUR QUERY HERE');

您可以指定完整的查询,并且

$this->db->select()

它是 ActiveRecord 的一部分,您可以指定要选择的字段

I think with

$this->db->query('YOUR QUERY HERE');

you can specify the complete query and with

$this->db->select()

which is part of ActiveRecord you specify the fields to be selected

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