[英]How to Get Error Details From MySQL Stored Procedure
我正在使用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';
那可以告訴我哪個表的哪一列是未知的。
創建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
我發現這對我有幫助。 “ 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
}
ps我知道這有點晚了,但也許可以幫助某人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.