Codeigniter simple_query与查询构建器(插入,更新和删除) [英] Codeigniter simple_query vs. query builder (insert,update and delete)

查看:206
本文介绍了Codeigniter simple_query与查询构建器(插入,更新和删除)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,simple_query不会返回任何数据库结果集,也不会设置查询计时器,编译绑定数据或存储查询以进行调试.

As per documentation, simple_query will not return any database result set, nor does it set the query timer, or compile bind data, or store your query for debugging.

就像在CodeIgniter中一样,我正在使用CI提供的查询生成器来生成查询.

As in my CodeIgniter I am using query builder provided by CI to generate queries.

那么,如果这些用于插入,更新和删除的查询构建器的工作方式与simple_query相同,或者它们在后台的工作方式不同?

So if these query builders for insert, update and delete works in the same way like simple_query or they work differently in the background?

推荐答案

simple_query()是CodeIgniter中唯一行为如您所指出的数据库方法.如文档所述:大多数用户很少使用此功能."

simple_query() is the only database method in CodeIgniter that behaves as you have pointed out. As the documentation states: "Most users will rarely use this function."

除少数例外,所有其他查询生成器方法均返回DB_query_builder实例和CI_DB_result对象,或者对于写"类型查询,返回指示成功或失败的布尔值.少数例外会传回整数,字串或混合(值或FALSE).

With a few exceptions, all other Query Builder methods return either a DB_query_builder instance a CI_DB_result object or - in the case of "write" type queries - a Boolean that indicates success or failure. The few exceptions return an integer, string or mixed (a value or FALSE).

所有接受输入值的方法都会转义(或选择不转义)所提供的值.

All methods that accept input values escape (or optionally not escape) the values provided.

虽然Query Builder(QB)是一个很棒的工具,但通常没有必要.使用$this->db->query('your statement here');通常更有效.理解QB的目标是创建一个字符串,该字符串实际上是在对db->query('a query string');的调用中使用的.

While Query Builder (QB) is a great tool it is often not necessary. Using $this->db->query('your statement here'); is frequently more efficient. Understand the goal of QB is to create a string that is literally used in a call to db->query('a query string');.

所以不用输入所有这些内容...

So instead of typing all this...

$this->db->select('id, name, email');
$this->db->from('customers');
$this->db->where('id', $id)
$this->db->where('active', $is_active)
$query = $this->get();
$result = $query->result();

键入以下命令会产生与上面完全相同的结果,因为它直接提供了QB在上面的代码中构建的查询字符串. (查询也完全转义了.)但是它执行的代码少了很多. (输入较少.)

Typing the following produces the exact same results as the above because it directly provides the query string that QB built in the above code. (The query is fully escaped too.) But it executes a ton less code to get there. (With Less typing.)

$query = $this->db->query("Select id, name, email from customers where id = ? and active = ?", [$id, $is_active]);
$result = $query->result();

这是使用查询绑定

研究核心源代码(通常在驱动程序"文件中)将为您显示在何处使用simple_query()是合适和有用的.

Studying the core source code (mostly in 'driver' files) will show you where using simple_query() is appropriate and useful.

这篇关于Codeigniter simple_query与查询构建器(插入,更新和删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆