问题描述
让对PDO的引用等于一个PDO对象,而不是PDO本身-
我在此处看到同时存在PDO->query()
和PDO->exec()
.在链接的页面中,似乎PDO->query();
仅用于SELECT
语句,而PDO->exec()
仅用于UPDATE
,INSERT
,DELETE
语句.现在,我是PDO的新手,所以不确定还的使用情况,所以我希望您能解释为什么使用不同的方法,以及为什么使用PDO有不同的方法.
I see here that there is both PDO->query()
and PDO->exec()
. In the page that was linked, it appears that PDO->query();
is used for SELECT
statements ONLY, and PDO->exec()
is used for UPDATE
,INSERT
,DELETE
statements. Now, I am brand new to PDO, so I'm not sure what is going on in terms of using it quite yet, so I would appreciate an explanation on why to use the different methods, and why there are different methods.
推荐答案
尽管存在理论上的差异,但无论如何都不应使用这些功能-因此,无需担心.
Despite of whatever theoretical difference, none of these functions should be used anyway - so, there is nothing to concern of.
使用PDO的唯一原因是支持准备好的语句,但是这些功能都没有提供它.因此,不应使用它们.
The only reason of using PDO is support for prepared statements, but none of these functions offers it. So, they shouldn't be used.
使用prepare()/execute()
代替,尤其是用于UPDATE,INSERT,DELETE语句.
Use prepare()/execute()
instead, especially for UPDATE,INSERT,DELETE statements.
请注意,尽管准备好的声明被广泛宣传为一种安全措施,但这只是为了引起人们的注意.但是,它们的 real 目的是正确的查询格式.这也为您提供了安全性-格式正确的查询也无法注入-就像副作用一样.但是,再次说明-格式化是主要目标,因为即使格式正确的数据,即使是无辜的数据也可能导致查询错误.
Please note that although prepared statements are widely advertised as a security measure, it is only to attract people's attention. But their real purpose is proper query formatting. Which gives you security too - as properly formatted query cannot be injected as well - just as side effect. But again - formatting is a primary goal, just because even innocent data may cause a query error if not formatted properly.
请注意,execute()
仅返回TRUE
或FALSE
以指示操作成功.对于其他信息,例如受UPDATE
影响的记录数,提供了诸如rowCount()
的方法.请参见文档.
Please note that execute()
returns only TRUE
or FALSE
to indicate success of the operation. For other information, such as the number of records affected by an UPDATE
, methods such as rowCount()
are provided. See the docs.
这篇关于PDO-> query()和PDO-> exec()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!