php教程--案例37(PDO错误处理机制)

<?php
//case 37 PDO错误处理机制
/**
 * 建表sql语句
 *create table emp_books(
book_id int unsigned auto_increment primary key,
book_name varchar(20) not null comment '书名',
book_author varchar(15) comment '作者',
pub_time varchar(10) comment '出版日期'
)charset = utf8;
 *
 * insert into emp_books values(1,'PHP程序设计基础教程','虾米大王产品研发部','2020/08');
insert into emp_books values(2,'PHP程序设计高级教程','虾米大王产品研发部','2020/09');
insert into emp_books values(3,'MySQL数据库入门教程','虾米大王产品研发部','2020/07');
insert into emp_books values(4,'Java入门基础','虾米大王产品研发部','2020/01');
 */

header('content-type:text/html;charset=utf-8');
try
{
    $pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8;','root','mysql123');
    //处理方式一
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT);
    //处理方式二
    //$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
    //处理方式三
    //$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

    $stmt = $pdo->prepare('select * from `xxx`');
    $stmt->execute();
    echo '第一种处理方式......';
    $code = $stmt->errorCode();
    if(!empty($code))
    {
        echo "<br>$code<br>";
        print_r($stmt->errorInfo());
    }
}
catch (PDOException $ex)
{
    echo $ex->getMessage().'<br>';
}


 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页