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

查看:74
本文介绍了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");

.那么,我应该使用哪一个-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.

  • http://www.php.net/manual/en/pdo.query.php
  • http://php.net/manual/en/pdo.exec.php

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

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

  • 安装PDO_MYSQL
  • MYSQL_ATTR_INIT_COMMAND替换为1002
  • 将您的PHP更新到已传递和修补的最新稳定版本.
  • Install PDO_MYSQL
  • Replace MYSQL_ATTR_INIT_COMMAND with 1002
  • Update your PHP To the latest stable release where it has been passed and patched.

第二个问题可能与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天全站免登陆