php PDO::exec() 还是 PDO::query()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4978481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:25:20  来源:igfitidea点击:

PDO::exec() or PDO::query()?

phputf-8pdo

提问by Dmitri

I used to have this as one of the options (4th param) passed to PDO constructor:

我曾经将此作为传递给 PDO 构造函数的选项(第 4 个参数)之一:

$aOptions[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES utf8";

But just found that it does not work on certain php versions on Windows (does not work in 5.3) due to some bug.

但是刚刚发现由于某些错误,它在 Windows 上的某些 php 版本上不起作用(在 5.3 中不起作用)。

Now I need to run SET NAMES utf8using either $pdo->exec("SET NAMES utf8");

现在我需要SET NAMES utf8使用$pdo->exec("SET NAMES utf8");

or $pdo->query("SET NAMES utf8");

或者 $pdo->query("SET NAMES utf8");

right after the instantiating the pdo object. So, which one should I use - exec() or query()?

在实例化 pdo 对象之后。那么,我应该使用哪一个——exec() 或 query()?

回答by RobertPitt

When using PDO::EXECthe result returned is not of an PDOStatementbut an integer of the rows affected.

使用时PDO::EXEC返回的结果不是PDOStatement受影响的行数,而是整数。

When using PDO::QUERYthe result returned is a PDOStatement.

使用时PDO::QUERY返回的结果是一个PDOStatement.

So the answer is it depends on what you need to do with the data, if you need to run query and not do anything with the results, then you should use execto execute the query, otherwise if you need the number of rows, the data returned you should use pdo::queryand then use the results returned by the call.

所以答案是这取决于你需要对数据做什么,如果你需要运行查询而不对结果做任何事情,那么你应该使用exec来执行查询,否则如果你需要行数,数据返回您应该使用pdo::query,然后使用调用返回的结果。



in regards to the bug there are several work around that you can take

关于该错误,您可以采取几种解决方法

  • Install PDO_MYSQL
  • Replace MYSQL_ATTR_INIT_COMMANDwith 1002
  • Update your PHP To the latest stable release where it has been passed and patched.
  • 安装 PDO_MYSQL
  • 替换MYSQL_ATTR_INIT_COMMAND1002
  • 将您的 PHP 更新到已通过并修补的最新稳定版本。

the second issue may have some issues on 64bit's OS's and Some windows configurations.

第二个问题可能在 64 位操作系统和某些 Windows 配置上存在一些问题。

Bug Information: http://bugs.php.net/bug.php?id=47224

错误信息:http: //bugs.php.net/bug.php?id=47224

回答by Jeremy

PDO::exec() should be used for queries that do not return a resultset, such as a delete statement or 'set'. PDO::query() should be used when you expect a resultset to be returned. It returns a PDOStatement object to you that you can iterate over to get the individual rows. Note though that if you're using data from an untrusted source in your queries prepared statements would be the best way to go for either kind of query (but you probably knew that).

PDO::exec() 应该用于不返回结果集的查询,例如删除语句或“设置”。当您期望返回结果集时,应使用 PDO::query()。它向您返回一个 PDOStatement 对象,您可以遍历该对象以获取各个行。请注意,如果您在查询中使用来自不受信任来源的数据,则准备好的语句将是进行任何一种查询的最佳方式(但您可能知道这一点)。

So, in your case PDO::exec() would be right. Are you sure though that passing the set names command to PDO::__construct() as the last value doesn't work? It works for me and I have PHP 5.3 on Windows. Could you post some more sample code of what you're doing?

所以,在你的情况下 PDO::exec() 是正确的。您确定将 set names 命令传递给 PDO::__construct() 作为最后一个值不起作用吗?它对我有用,我在 Windows 上有 PHP 5.3。你能发布更多关于你在做什么的示例代码吗?