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

PDO::exec() or PDO::query()?(PDO::exec() 还是 PDO::query()?)
本文介绍了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.

  • http://www.php.net/manual/en/pdo.查询.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 更新到已通过并修补的最新稳定版本.

第二个问题可能在 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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)