一.PDO异常处理
PDO::ATTR_ERRMODE
1) PDO::ATTR_ERRMODE//不报错误(忽略)(0)
2) PDO::ERRMODE_WARNING //以警告的方式报错(1)
3) PDO::ERRMODE_EXCEPTION //以异常的方式报错(2)
<?php
//默认是PDO::ATTR_ERRMODE 不报错误(忽略)(0),需要用errorCode()、errorInfo()
try{
$pdo=new PDO("mysql:host=localhost;dbname=myapp","root","");
// $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e){
die("fail to connect db".$e->getMessage());
}
$sql="INSERT INTO user VALUES(null,'dabao','26')";
try{
$res=$pdo->exec($sql);
}catch (PDOException $e){
echo $e->getMessage();
}
//$res=$pdo->exec($sql);
//if($res){
// echo 'OK';
//}else{
// echo $pdo->errorCode();
// echo '<br/>';
// print_r($pdo->errorInfo());
//}
二.PDO预处理方法
1) prepare() //用于执行查询SQL语句,返回PDOStatement对象2) bindValue() //将值绑定到对应的一个参数,返回布尔值
3) bindParam() //将参数绑定到相应的查询占位符上,返回布尔值
4) bindColumn() //用来匹配列名和一个指定的变量名
5) execute() // 执行一个准备好了的预处理语句,返回布尔值
6) rowCount() // 返回使用增、删、改、查操作语句后受影响的行总数
<?php
/**
* ?号式的预处理语句,共有三种绑定方式
*/
//1.连接数据库
try{
$pdo=new PDO("mysql:host=localhost;dbname=myapp","root","");
}catch (PDOException $e){
die("fail to connect db".$e->getMessage());
}
//2.预处理的SQL语句
$sql="INSERT INTO users(id,name,age) VALUES(?,?,?)";
$stmt=$pdo->prepare($sql);
//3.对?号的参数进行绑定
$id=null;
$name="test103";
$age=103;
//第一种绑定方式
//$stmt->bindValue(1,$id);
//$stmt->bindValue(2,$name);
//$stmt->bindValue(3,$age);
//第二种绑定方式
//$stmt->bindParam(1,$id);
//$stmt->bindParam(2,$name);
//$stmt->bindParam(3,$age);
//4.执行
//$stmt->execute();
//第三种绑定方式:直接执行数组
$stmt->execute(array($id,$name,$age));
echo $stmt->rowCount();
<?php
/**
* 别名式的预处理语句,共有三种绑定方式
*/
//1.连接数据库
try{
$pdo=new PDO("mysql:host=localhost;dbname=myapp","root","");
}catch (PDOException $e){
die("fail to connect db".$e->getMessage());
}
//2.预处理的SQL语句
$sql="INSERT INTO users(id,name,age) VALUES(:id,:name,:age)";
$stmt=$pdo->prepare($sql);
//3.参数进行绑定
$id=null;
$name="test203";
$age=23;
//第一种绑定方式
//$stmt->bindValue("id",$id);
//$stmt->bindValue("name",$name);
//$stmt->bindValue("age",$age);
//第二种绑定方式
//$stmt->bindParam("id",$id);
//$stmt->bindParam("name",$name);
//$stmt->bindParam("age",$age);
//4.执行
//$stmt->execute();
//第三种绑定方式:直接执行数组
$stmt->execute(array("id"=>$id,"name"=>$name,"age"=>$age));
echo $stmt->rowCount();
<?php
/**
* 用预处理方式查询数据
*/
//1.连接数据库
try{
$pdo=new PDO("mysql:host=localhost;dbname=myapp","root","");
}catch (PDOException $e){
die("fail to connect mysql".$e->getMessage());
}
//2.预处理查询
$sql="SELECT id,name,age FROM users";
$stmt=$pdo->prepare($sql);
//3.执行
$stmt->execute();
foreach($stmt as $val){
echo $val['id']."------".$val['name']."------".$val['age']."<br/>";
}
三.事务处理操作方法介绍
1) beginTransaction() //开启一个事物(做一个回滚点)
2) commit() //提交事务
3) rollBack() //事务回滚操作
<?php
//1.连接数据库
try{
$pdo=new PDO("mysql:host=localhost;dbname=myapp","root","");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e){
die("fail to connect db".$e->getMessage());
}
//2.执行数据操作
try{
//开启事物
$pdo->beginTransaction();
$sql="insert into users(id,name,age) VALUES(?,?,?)";
$stmt=$pdo->prepare($sql);
//传入参数
$stmt->execute(array(null,"test1","21"));
$stmt->execute(array(null,"test2","22"));
$stmt->execute(array(null,"test3","23"));
//提交事物
$pdo->commit();
}catch (PDOException $e){
die("fail to execute".$e->getMessage());
//事物回滚
$pdo->roolback();
}