PHP

[PDO]⑥--查看错误信息

2017-09-01  本文已影响0人  子木同

$pdo->errorCode()

$pdo->errorInfo()

<?php
header("content-type:text/html;charset=utf-8");
try {
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
    $sql = 'delete from user1 where id=2';
    $res = $pdo->exec($sql);
    if ($res == false) {
        //$pdo->errorCode();SQLSTATE的值
        echo $pdo->errorCode() . "<br/>";//42S02
        //$pdo->errorInfo() 返回的错误信息的数组 数组中包含三个单元
        //SQLSTATE,CODE,INFO
        var_dump($pdo->errorInfo());
        /**
         * array
         *0 => string '42S02' (length=5)
         *1 => int 1146
         *2 => string 'Table 'test.user1' doesn't exist' (length=32)
         */
    }

} catch (PDOException $e) {
    echo $e->getMessage();
}

?>
上一篇下一篇

猜你喜欢

热点阅读