您当前的位置: 首页 > 学无止境 > 网站建设 网站首页网站建设
使用PDO进行SQL语句预处理和操作结果集
发布时间:2015-12-04 12:48:34编辑:雪饮阅读()
pdo预处理sql新增数据(关联方式绑定参数):
<?php
try{
$pdo=new PDO("mysql:host=localhost;dbname=test2","root","root");
}
catch(PDOException $e){
echo $e->getMessage();
}
/*
pdo的两种占位符号:
索引:用"?"作为占位符
关联:和关联数组一样通过变量绑定指定参不用管参数顺序
*/
//准备预处理sql
$stmt=$pdo->prepare("insert into shops(name,price,num,desn) values(:name,:price,:num,:desn)");
//绑定参数(关联方式)
$stmt->bindParam(":name",$name);
$stmt->bindParam(":price",$price);
$stmt->bindParam(":num",$num);
$stmt->bindParam(":desn",$desn);
$name="th";
$price=12.56;
$num=12;
$desn='xiaotianhu';
if($stmt->execute()){
echo "执行成功";
}
else{
echo "执行失败";
}
?>
pdo预处理sql新增数据(索引方式绑定参数):
<?php
try{
$pdo=new PDO("mysql:host=localhost;dbname=test2","root","root");
}
catch(PDOException $e){
echo $e->getMessage();
}
/*
pdo的两种占位符号:
索引:用"?"作为占位符
关联:和关联数组一样通过变量绑定指定参不用管参数顺序
*/
//准备预处理sql
$stmt=$pdo->prepare("insert into shops(name,price,num,desn) values(?,?,?,?)");
//绑定参数(索引方式)
$stmt->bindParam(1,$name);
$stmt->bindParam(2,$price);
$stmt->bindParam(3,$num);
$stmt->bindParam(4,$desn);
$name="th";
$price=12.56;
$num=12;
$desn='xiaotianhusuoyin';
if($stmt->execute()){
echo "执行成功";
}
else{
echo "执行失败";
}
?>
绑定参数时还可以添加参数类型如int,str等:
$stmt->bindParam(1,$name,PDO::PARAM_STR);
$stmt->bindParam(2,$price,PDO::PARAM_STR);
$stmt->bindParam(3,$num,PDO::PARAM_INT);
$stmt->bindParam(4,$desn,PDO::PARAM_STR);
pdo中预处理数据成功后也可以获取最后插入id:
if($stmt->execute()){
echo "执行成功<br/>";
echo "最后插入id:".$pdo->lastInsertId();
}
else{
echo "执行失败";
}
pdo预处理更新数据库:
<?php
try{
$pdo=new PDO("mysql:host=localhost;dbname=test2","root","root");
}
catch(PDOException $e){
echo $e->getMessage();
}
//准备预处理sql
$stmt=$pdo->prepare("update shops set name=:name,price=:price,num=:num,desn=:desn where id=:id");
//绑定参数(索引方式)
$stmt->bindParam(":name",$name);
$stmt->bindParam(":price",$price);
$stmt->bindParam(":num",$num);
$stmt->bindParam(":desn",$desn);
$stmt->bindParam(":id",$id);
$name="thupdate";
$price=18.56;
$num=19;
$desn='woshigengxinleixing';
$id=1;
if($stmt->execute()){
echo "执行成功<br/>";
}
else{
echo "执行失败";
}
?>
pdo免绑定(数组绑定)直接执行:
<?php
try{
$pdo=new PDO("mysql:host=localhost;dbname=test2","root","root");
}
catch(PDOException $e){
echo $e->getMessage();
}
//准备预处理sql
$stmt=$pdo->prepare("insert into shops(name,price,num,desn) values(:name,:price,:num,:desn)");
//绑定参数(索引方式)
$stmt->execute(array(":price"=>99,":name"=>"kkk1",":num"=>4512,":desn"=>"aaa1"));
$stmt->execute(array(":price"=>99.2,":name"=>"kkk2",":num"=>4513,":desn"=>"aaa12"));
$stmt->execute(array(":price"=>99.3,":name"=>"kkk3",":num"=>4514,":desn"=>"aaa13"));
$stmt->execute(array(":price"=>99.4,":name"=>"kkk4",":num"=>4515,":desn"=>"aaa14"));
$name="thupdate";
$price=18.56;
$num=19;
$desn='woshigengxinleixing';
$id=1;
if($stmt->execute()){
echo "执行成功<br/>";
}
else{
echo "执行失败";
}
?>
这样以来就可以在使用ajax或任何表单提交过来的方式将整个表单结果作为数组绑定如:
exccute($_POST);
pdo索引参数免绑定直接执行:
<?php
try{
$pdo=new PDO("mysql:host=localhost;dbname=test2","root","root");
}
catch(PDOException $e){
echo $e->getMessage();
}
//准备预处理sql
$stmt=$pdo->prepare("insert into shops(name,price,num,desn) values(?,?,?,?)");
//绑定参数(索引方式)
$stmt->execute(array("jbw",22.56,23,"miaoshu"));
$stmt->execute(array("jbw2",22.57,24,"miaoshu1"));
$stmt->execute(array("jbw3",22.58,25,"miaoshu2"));
$stmt->execute(array("jbw4",22.59,26,"miaoshu3"));
$name="thupdate";
$price=18.56;
$num=19;
$desn='woshigengxinleixing';
$id=1;
if($stmt->execute()){
echo "执行成功<br/>";
}
else{
echo "执行失败";
}
?>
最后的判断执行是否成功其实是有问题的,因为在判断时$stmt->execute()会继续执行一次,而这次就是默认的执行最后一次的绑定并执行的语句,这里就是执行:
$stmt->execute(array("jdw4",22.59,26,"miaoshu3"));
pdo取出sql结果集中的单条结果以及取出单条结果的数组方式:
<?php
try{
$pdo=new PDO("mysql:host=localhost;dbname=test2","root","root");
}
catch(PDOException $e){
echo $e->getMessage();
}
//准备预处理sql
$stmt=$pdo->prepare("select * from shops where id>:id order by id");
//绑定参数(索引方式)
$stmt->execute(array("id"=>18));
$row=$stmt->fetch(PDO::FETCH_NUM);//索引数组取出
print_r($row);
echo "<br/>";
$row=$stmt->fetch(PDO::FETCH_ASSOC);//关联数组取出
print_r($row);
echo "<br/>";
$row=$stmt->fetch(PDO::FETCH_BOTH);//关联与索引并存
print_r($row);
echo "<br/>";
$row=$stmt->fetch();//默认情况下关联与索引并存
print_r($row);
echo "<br/>";
if($stmt->execute()){
echo "执行成功<br/>";
}
else{
echo "执行失败";
}
?>
也可以全局设置fetch取出数组中单条数据数组的方式:
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$row=$stmt->fetch();
print_r($row);
echo "<br/>";
$row=$stmt->fetch();
print_r($row);
echo "<br/>";
$row=$stmt->fetch();
print_r($row);
通过while循环直接遍历出所有的记录:
while($row=$stmt->fetch()){
print_r($row);
echo "<br/>";
}
fecth仅仅只是每次从数据库中读取一条,fetchAll可以从数据库中取出整个结果集:
$data=$stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r($data);
echo "</pre>";
设置全局fetch抽取模式对fetchAll同样有效:
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$data=$stmt->fetchAll();
echo "<pre>";
print_r($data);
echo "</pre>";
pdo获取遍历结果集每条记录的所有字段值:
<?php
header("Content-type:text/html;charset=utf-8;");
try{
$pdo=new PDO("mysql:host=localhost;dbname=test2","root","root");
}
catch(PDOException $e){
echo $e->getMessage();
}
//准备预处理sql
$stmt=$pdo->prepare("select * from shops where id>:id order by id");
//绑定参数(索引方式)
$stmt->bindColumn("id",$id,PDO::PARAM_INT);
$stmt->bindColumn("name",$name,PDO::PARAM_STR);
$stmt->bindColumn("price",$price,PDO::PARAM_STR);
$stmt->bindColumn("num",$num,PDO::PARAM_INT);
$stmt->bindColumn("desn",$desn,PDO::PARAM_STR);
$stmt->execute(array(":id"=>18));
while($stmt->fetch()){
echo $id."----".$name."------".$price."-------".$num."-------".$desn."<br/>";
}
echo "记录总数:".$stmt->rowCount()."<br/>字段总数:".$stmt->columnCount()."<br/>";
if($stmt->execute()){
echo "执行成功<br/>";
}
else{
echo "执行失败";
}
?>
pdo遍历结果集每个字段的详细信息:
<?php
header("Content-type:text/html;charset=utf-8;");
try{
$pdo=new PDO("mysql:host=localhost;dbname=test2","root","root");
}
catch(PDOException $e){
echo $e->getMessage();
}
//准备预处理sql
$stmt=$pdo->prepare("select * from shops where id>:id order by id");
//绑定参数(索引方式)
$stmt->bindColumn("id",$id,PDO::PARAM_INT);
$stmt->bindColumn("name",$name,PDO::PARAM_STR);
$stmt->bindColumn("price",$price,PDO::PARAM_STR);
$stmt->bindColumn("num",$num,PDO::PARAM_INT);
$stmt->bindColumn("desn",$desn,PDO::PARAM_STR);
$stmt->execute(array(":id"=>18));
for($i=0;$i<$stmt->columnCount();$i++){
$fied=$stmt->getColumnMeta($i);
print_r($fied);
echo "<hr/>";
}
echo "记录总数:".$stmt->rowCount()."<br/>字段总数:".$stmt->columnCount()."<br/>";
if($stmt->execute()){
echo "执行成功<br/>";
}
else{
echo "执行失败";
}
?>
关键字词:pdo,sql,结果集