php pdo 捕获并输出 mysql 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12976697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:34:56  来源:igfitidea点击:

pdo catch and output mysql errors

phpmysqlpdotry-catch

提问by Smudger

Still trying to get my head around pdo.

仍然试图让我的头脑围绕 pdo。

I have an insert statement that is executed with pdo. insert works great however if there is an error I would like it displayed to the user.

我有一个用 pdo 执行的插入语句。插入效果很好,但是如果出现错误,我希望将其显示给用户。

I have the below try catch block.

我有下面的 try catch 块。

try{ 
        $insertuser = $db->prepare('INSERT INTO `she_she`.`Persons` (`idnumber`,`addedby`,`firstname`, `middlename`, `surname`, `fullname`, `gender`, `birthdate`, `homelanguage`, `department`, `employeetype`, `employeestatus`) VALUES  (?,?,?,?,?,?,?,?,?,?,?,?)'); 
        $insertuser->execute(array($idnumber,$user,$firstname, $middlename, $surname, $fullname, $gender, $birthdate, $language, $department, $employmenttype, $personstatus));  
    } 
    catch(PDOException $exception){ 
        return $exception; 
    } 

If the query fails, or lets say a duplicate IDNumber, I want this displayed to the user.

如果查询失败,或者说重复的 IDNumber,我希望将其显示给用户。

If I simply try echo the variable $exception it does not work.

如果我只是尝试 echo 变量 $exception 它不起作用。

I want to return the mysql error to the user.

我想将mysql错误返回给用户。

Any advice appreciated as always.

任何建议一如既往地受到赞赏。

Thanks, Ryan

谢谢,瑞安

UPDATE

更新

new code as per proposed answers:

根据建议的答案的新代码:

   try{ 
        $insertuser = $db->prepare('INSERT INTO `she_she`.`Persons` (`idnumber`,`addedby`,`firstname`, `middlename`, `surname`, `fullname`, `gender`, `birthdate`, `homelanguage`, `department`, `employeetype`, `employeestatus`) VALUES  (?,?,?,?,?,?,?,?,?,?,?,?)'); 
        $insertuser->execute(array($idnumber,$user,$firstname, $middlename, $surname, $fullname, $gender, $birthdate, $language, $department, $employmenttype, $personstatus));  
    } 
    catch(PDOException $exception){ 
       return $exception->getMessage(); 
    } 
echo "exception: ".$exception;

回答by Cody Covey

By default PDO is not in a state that will display errors. you need to provide the following in your DB connection

默认情况下,PDO 不处于会显示错误的状态。您需要在数据库连接中提供以下内容

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

More info can be seen Here

更多信息可以在这里看到

回答by Christiaan

You should use this:

你应该使用这个:

return $exception->getMessage();

See the page on the documentation of Exception class:

请参阅 Exception 类的文档页面:

http://www.php.net/manual/en/exception.getmessage.php

http://www.php.net/manual/en/exception.getmessage.php

回答by Ayhan Kesicio?lu

1.Add ERRMODE_EXCEPTION mode after your db connection:

1.在你的数据库连接后添加 ERRMODE_EXCEPTION 模式:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

2.And than you must use try{} catch{} method for all your mysql query. Like this:

2.而且您必须对所有 mysql 查询使用 try{} catch{} 方法。像这样:

try {
    $SQL = "DELETE FROM items WHERE item_id=:item_id";
    $m = $dbh->prepare($SQL);
    $m->bindParam(':item_id', $item_id, PDO::PARAM_INT);
    $m->execute();
    //success
    $return = "Your success message.";
}
catch (PDOException $e) {
    //error
    $return = "Your fail message: " . $e->getMessage();
}