Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO错误:$PDO->;准备()->;execute()抛出错误;对boolean“上的成员函数fetch()调用;_Php_Mysql_Pdo - Fatal编程技术网

Php PDO错误:$PDO->;准备()->;execute()抛出错误;对boolean“上的成员函数fetch()调用;

Php PDO错误:$PDO->;准备()->;execute()抛出错误;对boolean“上的成员函数fetch()调用;,php,mysql,pdo,Php,Mysql,Pdo,如果我的pdo查询类似于: $sql=$pdo->prepare('select timezone from wo_users where user_id=?')->execute(array($wo['user']['user_id'])); while($row=$sql->fetch()){var_dump($row);die;} 将抛出一个错误: (!)致命错误:调用成员函数 中的布尔值上的fetch() 第27行的C:\wamp64\www\community.vo

如果我的pdo查询类似于:

$sql=$pdo->prepare('select timezone from wo_users where user_id=?')->execute(array($wo['user']['user_id']));
while($row=$sql->fetch()){var_dump($row);die;}
将抛出一个错误:

(!)致命错误:调用成员函数 中的布尔值上的fetch() 第27行的C:\wamp64\www\community.voyance\2\sources\calendar.php

但如果直接使用query()的pdo查询不会抛出错误:

$sql=$pdo->query('select timezone from wo_users where user_id=1;');
while($row=$sql->fetch()){var_dump($row);die;}
C:\wamp64\www\community.voyance\2\sources\calendar.php:27:array (大小=1)“时区”=>字符串'-8'(长度=2)


为什么会这样?谢谢

您需要一个
pdo语句来获取,
execute
只返回true或false。您应该只执行
语句
。这将返回一个可以
获取的结果对象或一个错误。有关PDO错误处理,请参阅

正如您从手册中看到的,因为:

PDO::query()返回PDOStatement对象,失败时返回FALSE

其中:

成功时返回TRUE,失败时返回FALSE

这就是我们需要的:

如果数据库服务器成功准备语句,则PDO::prepare()将返回一个PDOStatement对象。如果数据库服务器无法成功准备语句,PDO::prepare()将返回FALSE或发出PDOException(取决于错误处理)

和(这是描述,不是返回):

从与PDOStatement对象关联的结果集中获取行


$sql=$pdo->prepare('select timezone from wo_users,其中user_id=?')$sql->execute(数组($wo['user']['user\u id']);请看较长的评论。它与每个函数调用返回的内容有关。克服麻烦
$stmt = $pdo->prepare('select timezone from wo_users where user_id=?');
$stmt->execute(array($wo['user']['user_id']));
while($row = $stmt->fetch()){
     var_dump($row);
     die;
}