我曾经把它作为传递给 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_MYSQLMYSQL_ATTR_INIT_COMMAND 替换为 1002第二个问题可能在 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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
MySQLi准备好的语句&foreach 循环MySQLi prepared statement amp; foreach loop(MySQLi准备好的语句amp;foreach 循环)
mysqli_insert_id() 是从整个服务器还是从同一用户获Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是从整个服务器还是从同一用户获取记录?)
PHP MySQLi 无法识别登录信息PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 无法识别登录信息)
mysqli_select_db() 需要 2 个参数mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 个参数)
Php mysql pdo 查询:用查询结果填充变量Php mysql pdo query: fill up variable with query result(Php mysql pdo 查询:用查询结果填充变量)
MySQLI 28000/1045 用户“root"@“localhost"的访问MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用户“root@“localhost的访问被拒绝)