PDO对SQL查询错误的处理模式之--异常模式 用过PDO的高手请进 20
<?phpheader("content-type:text/html;charset=utf-8");$dsn="mysql:host=localhost;dbname...
<?php
header("content-type:text/html;charset=utf-8");
$dsn="mysql:host=localhost;dbname=ztlibrary";
$user="root";
$password="root";
$dbh=new PDO($dsn,$user,$password);
try
{
$dbh->query("set names utf8");
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="select Idcode,Book_name from Book_info";
$stmt=$dbh->prepare($sql);
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
echo $row['Id_code']."=".$row['Book_name']."<br/>";
}
}catch(PDOException $e)
{
echo "PDO Exception Caught.";
echo "Error with the database:<br />";
echo "SQL Query:",$sql;
echo "<pre>";
echo "Error:".$e->getMessage()."<br />";
echo "Code:".$e->getCode()."<br />";
echo "File:".$e->getFile()."<br />";
echo "Line:".$e->getLine()."<br />";
echo "Trace:".$e->getTraceAsString();
echo "</pre>";
}
/**
* 学习笔记
*
* 为什么错误提示没有捕获异常?
* 我理解的是try语句块里面如果发生错误,就直接执行catch语句块的代码输出的内容应该是:
* PDO Exception Caught.Error with the database:
*SQL Query:select Idcode,Book_name from Book_info
*Error:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Idcode' in 'field list'Code:42S22
*File:D:\phpnow\htdocs\PDOstudy\question_1.php
*Line:13
*Trace:#0 D:\phpnow\htdocs\PDOstudy\question_1.php(13): PDOStatement->execute()
*#1 {main}
*
*可是运行结果是
* Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Idcode' in 'field list'' in D:\phpnow\htdocs\PDOstudy\question_1.php:
*13 Stack trace: #0 D:\phpnow\htdocs\PDOstudy\question_1.php(13): PDOStatement->execute() #1 {main} thrown in D:\phpnow\htdocs\PDOstudy\question_1.php on line 13
*
*明明是捕获了异常,为什么还出现这个错误?*/
?> 展开
header("content-type:text/html;charset=utf-8");
$dsn="mysql:host=localhost;dbname=ztlibrary";
$user="root";
$password="root";
$dbh=new PDO($dsn,$user,$password);
try
{
$dbh->query("set names utf8");
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="select Idcode,Book_name from Book_info";
$stmt=$dbh->prepare($sql);
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
echo $row['Id_code']."=".$row['Book_name']."<br/>";
}
}catch(PDOException $e)
{
echo "PDO Exception Caught.";
echo "Error with the database:<br />";
echo "SQL Query:",$sql;
echo "<pre>";
echo "Error:".$e->getMessage()."<br />";
echo "Code:".$e->getCode()."<br />";
echo "File:".$e->getFile()."<br />";
echo "Line:".$e->getLine()."<br />";
echo "Trace:".$e->getTraceAsString();
echo "</pre>";
}
/**
* 学习笔记
*
* 为什么错误提示没有捕获异常?
* 我理解的是try语句块里面如果发生错误,就直接执行catch语句块的代码输出的内容应该是:
* PDO Exception Caught.Error with the database:
*SQL Query:select Idcode,Book_name from Book_info
*Error:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Idcode' in 'field list'Code:42S22
*File:D:\phpnow\htdocs\PDOstudy\question_1.php
*Line:13
*Trace:#0 D:\phpnow\htdocs\PDOstudy\question_1.php(13): PDOStatement->execute()
*#1 {main}
*
*可是运行结果是
* Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Idcode' in 'field list'' in D:\phpnow\htdocs\PDOstudy\question_1.php:
*13 Stack trace: #0 D:\phpnow\htdocs\PDOstudy\question_1.php(13): PDOStatement->execute() #1 {main} thrown in D:\phpnow\htdocs\PDOstudy\question_1.php on line 13
*
*明明是捕获了异常,为什么还出现这个错误?*/
?> 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询