PDO错误未捕获异常SQLSTATE [HY000]

时间:2013-02-19 05:06:52

标签: php pdo

我一直收到这个错误,但我不知道为什么......

错误是:

Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]:

General error' in test.php:25\nStack trace:\n#0 test.php(25): 

PDOStatement->fetch()\n#1 {main}\n  thrown in test.php on line 25

我的查询是这样的:

$stmt = $pdo->prepare("

SELECT *,t1.id AS theID FROM users
   t1 LEFT JOIN users_settings t2
ON t1.id=t2.tid                 
   INNER JOIN extra_settings t3
ON t2.bid=t3.id");

try {
    $stmt->execute();
} catch (PDOException $e) {
    echo $e -> getMessage();  
}

while($row = $stmt->fetch()){ //error is here
    //do stuff
}

脚本可以正常运行,但无论如何都会显示错误= /

错误是什么意思,我该如何解决?

1 个答案:

答案 0 :(得分:0)

那是因为你必须在try语句中运行while循环:

try {
    $stmt->execute();
    while($row = $stmt->fetch()){
        //do stuff
    }
} 
catch (PDOException $e) {
    echo $e -> getMessage();  
}

您看到此消息是因为您的查询有问题。尝试:

SELECT *,t1.id AS theID FROM users
   t1 LEFT JOIN t2.users_settings
   ON t1.id=t2.tid                 
   INNER JOIN t3.extra_settings
   ON t2.bid=t3.id