PDO::exec() 还是 PDO::query()? [英] PDO::exec() or PDO::query()?

查看:20
本文介绍了PDO::exec() 还是 PDO::query()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

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

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

推荐答案

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

When using PDO::EXEC the result returned is not of an PDOStatement but an integer of the rows affected.

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

When using PDO::QUERY the result returned is a PDOStatement.

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

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 exec to execute the query, otherwise if you need the number of rows, the data returned you should use pdo::query and then use the results returned by the call.

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

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

  • 安装PDO_MYSQL
  • MYSQL_ATTR_INIT_COMMAND 替换为 1002
  • 将您的 PHP 更新到已通过并修补的最新稳定版本.

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

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

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

这篇关于PDO::exec() 还是 PDO::query()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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