本文實(shí)例講述了php實(shí)現(xiàn)PDO中捕獲SQL語句錯(cuò)誤的方法。分享給大家供大家參考,具體如下:
使用默認(rèn)模式-----PDO::ERRMODE_SILENT
在默認(rèn)模式中設(shè)置PDOStatement對象的errorCode屬性,但不進(jìn)行其他任何操作。
例如:
通過prepare()和execute()方法向數(shù)據(jù)庫中添加數(shù)據(jù),設(shè)置PDOStatement對象的erroCode屬性,手動(dòng)檢測代碼中的錯(cuò)誤,操作步驟如下。
$dbms='mysql';//數(shù)據(jù)庫類型$dbName='admin';//使用的數(shù)據(jù)庫$user='root';//數(shù)據(jù)庫連接用戶名$pwd='password';//數(shù)據(jù)庫連接密碼$host='localhost';//數(shù)據(jù)庫主機(jī)名$dsn="$dbms:host=$host;port=3306;dbname=$dbName";$pdo=new PDO($dsn,$user,$pwd);//初始化一個(gè)PDO對象,就是創(chuàng)建了數(shù)據(jù)庫連接對象$pdo$query="insert into user (username,password) values('admin')";//需要執(zhí)行的sql語句$res=$pdo->prepare($query);$res->execute();$code=$res->errorCode();echo $code.'<br>';if($code==00000){//如果沒有任何錯(cuò)誤, errorCode() 返回的是: 00000 ,否則就會返回一些錯(cuò)誤代碼echo "數(shù)據(jù)添加成功";}else{echo "數(shù)據(jù)庫錯(cuò)誤:<br>";echo 'SQL Query:'.$query;echo '<pre>';var_dump($res->errorInfo());echo '<pre>';}
運(yùn)行結(jié)果如下
21S01數(shù)據(jù)庫錯(cuò)誤:SQL Query:insert into user (username,password) values('admin')array(3) { [0]=> string(5) "21S01" [1]=> int(1136) [2]=> string(47) "Column count doesn't match value count at row 1"}使用警告模式-----PDO::ERRMODE_WARNING
警告模式會產(chǎn)生一個(gè)PHP警告,并設(shè)置errorCode屬性。如果設(shè)置的是警告模式,那么除非明確的檢查錯(cuò)誤代碼,否則程序?qū)⒗^續(xù)按照其方式運(yùn)行。
例如:
設(shè)置警告模式,通過prepare()和execute()方法讀取數(shù)據(jù)庫中數(shù)據(jù),并且通過while語句和fetch()方法完成數(shù)據(jù)的循環(huán)輸出,體會在設(shè)置成警告模式后執(zhí)行錯(cuò)誤的SQL的語句。
$dbms='mysql';//數(shù)據(jù)庫類型$dbName='admin';//使用的數(shù)據(jù)庫$user='root';//數(shù)據(jù)庫連接用戶名$pwd='password';//數(shù)據(jù)庫連接密碼$host='localhost';//數(shù)據(jù)庫主機(jī)名$dsn="$dbms:host=$host;port=3306;dbname=$dbName";try {$pdo = new PDO($dsn, $user, $pwd);//初始化一個(gè)PDO對象,就是創(chuàng)建了數(shù)據(jù)庫連接對象$pdo$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);//設(shè)置為警告模式$query = "select * from userrr";//需要執(zhí)行的sql語句$res = $pdo->prepare($query);//準(zhǔn)備查詢語句$res->execute();while ($result = $res->fetch(PDO::FETCH_ASSOC)) {//while循環(huán)輸出查詢結(jié)果集并設(shè)置結(jié)果集以關(guān)聯(lián)數(shù)組的形式返回。echo $result['id'] . " " . $result['username'] . " " . $result['password'];  }}catch(PDOException $e){die("ERROR!:".$e->getMessage().'<br>');}echo "繼續(xù)繼續(xù)繼續(xù)繼續(xù)繼續(xù)繼續(xù)繼續(xù)";運(yùn)行結(jié)果如下:
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'admin.userrr' doesn't exist in D:/wampserver/www/test/test/index1.php on line 14 繼續(xù)繼續(xù)繼續(xù)繼續(xù)繼續(xù)繼續(xù)繼續(xù)
可以看出在設(shè)置警告模式后,如果SQL語句出現(xiàn)錯(cuò)誤將給出一個(gè)提示信息,但是程序仍能夠繼續(xù)執(zhí)行下去。
使用異常模式----PDO::ERRMODE_EXCEPTION
異常模式將會創(chuàng)建一個(gè)PDOException,并設(shè)置errorCode屬性,它可以將執(zhí)行代碼封裝到一個(gè)try{}catch{}語句塊中。未捕獲的異常將會導(dǎo)致腳本中斷,并顯示堆棧跟蹤讓用戶了解是哪里出現(xiàn)了問題。
例如:
刪除一個(gè)錯(cuò)誤的數(shù)據(jù)表中的信息
$dbms='mysql';//數(shù)據(jù)庫類型$dbName='admin';//使用的數(shù)據(jù)庫$user='root';//數(shù)據(jù)庫連接用戶名$pwd='password';//數(shù)據(jù)庫連接密碼$host='localhost';//數(shù)據(jù)庫主機(jī)名$dsn="$dbms:host=$host;port=3306;dbname=$dbName";try {$pdo = new PDO($dsn, $user, $pwd);//初始化一個(gè)PDO對象,就是創(chuàng)建了數(shù)據(jù)庫連接對象$pdo$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//設(shè)置異常模式$query = "delete from userrr where id=1";//需要執(zhí)行的sql語句$res = $pdo->prepare($query);//準(zhǔn)備刪除語句$res->execute();}catch(PDOException $e){echo 'PDO Exception Caught: ';echo "Error with the database:<br>";echo 'SQL Query:'.$query;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().'<br>';echo '</pre>';}運(yùn)行結(jié)果:
PDO Exception Caught: Error with the database:SQL Query:delete from userrr where id=1ERROR:SQLSTATE[42S02]: Base table or view not found: 1146 Table 'admin.userrr' doesn't existCode:42S02File:D:/wampserver/www/test/test/index1.phpLine:14Trace:#0 D:/wampserver/www/test/test/index1.php(14): PDOStatement->execute()#1 {main}希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選