我在PHP中使用PDO调用mysql存储过程
try {
$conn = new PDO("mysql:host=$host_db; dbname=$name_db", $user_db, $pass_db);
$stmt = $conn->prepare('CALL sp_user(?,?,@user_id,@product_id)');
$stmt->execute(array("user2", "product2"));
$stmt->setFetchMode(PDO::FETCH_COLUMN, 0);
$errors = $stmt->errorInfo();
if($errors){
echo $errors[2];
}else{
/*Do rest*/
}
}catch(PDOException $e) {
echo "Error : ".$e->getMessage();
}
返回错误,因为插入查询中的字段名称错误
Unknown column 'name1' in 'field list'
所以我想知道是否有可能获得详细的错误信息: -
Unknown column 'Tablename.name1' in the 'field list';
可以告诉我哪个表是哪个表是未知的。
答案 0 :(得分:0)
在创建pdo连接时,传递错误模式和编码等选项: PDO系统由3个类组成:PDO,PDOStatement& PDOException。 PDOException 类是错误处理所需的。
以下是一个例子:
try
{
// use the appropriate values …
$pdo = new PDO($dsn, $login, $password);
// any occurring errors wil be thrown as PDOException
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$ps = $pdo->prepare("SELECT `name` FROM `fruits` WHERE `type` = ?");
$ps->bindValue(1, $_GET['type']);
$ps->execute();
$ps->setFetchMode(PDO::FETCH_COLUMN, 0);
$text = "";
foreach ($ps as $row)
{
$text .= $row . "<br>";
}
// a function or method would use a return instead
echo $text;
} catch (Exception $e) {
// apologise
echo '<p class="error">Oops, we have encountered a problem, but we will deal with it. Promised.</p>';
// notify admin
send_error_mail($e->getMessage());
}
// any code that follows here will be executed
答案 1 :(得分:0)
我发现这对我有帮助。 “error_log”打印到php错误日志,但您可以用任何您想要显示错误的方式替换。
} catch (PDOException $ex){
error_log("MYSQL_ERROR"); //This reminds me what kind of error this actually is
error_log($ex->getTraceAsString()); // will show the php file line (and parameter
// values) so you can figure out which
// query/values caused it
error_log($ex->getMessage()); // will show the actual MYSQL query error
// e.g. constraint issue
}
P.S。我知道这有点晚了但也许它可以帮助别人。