gpt4 book ai didi

php - 捕获 mysql pdo 错误

转载 作者:行者123 更新时间:2023-11-29 05:33:25 24 4
gpt4 key购买 nike

我正在努力掌握 pdo。我有以下功能:

  function get_editusers($db)  
{
$result = $db->query("SELECT firstname, middlename,surname,fullname,gender, birthdate,homelanguage,department,employeetype,employeestatus,idnumber FROM Persons where employeeid='$employeeid'");
return $result;
}
$useredit = get_editusers($db);

我如何知道查询是否正确运行?如果不成功,我希望执行某个操作,但不确定如何“捕获”错误。

if($useredit="failed"){ action if failed} else { action if true}

提前致谢

最佳答案

function get_editusers($db){    
try {
$result = $db->query("SELECT firstname, middlename,surname,fullname,gender, birthdate,homelanguage,department,employeetype,employeestatus,idnumber FROM Persons where employeeid='$employeeid'");
return $result;
} catch(PDOException $ex) {
return $ex;
}
}

$result = get_editusers($db);
if(isset($result->errorInfo)){
die($result);
}

关于php - 捕获 mysql pdo 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12800783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com