您当前的位置: 首页 > 学无止境 > 网站建设 网站首页网站建设
mysqli多语句处理与事务处理
发布时间:2015-11-30 11:46:41编辑:雪饮阅读()
Mysqli执行多条sql:
<?php
$mysqli=new MySQLi("localhost","root","root","mysqlicn");
$sql="insert into cgs(charu,gengxin,shanchu) values('sqlcr','sqlgx','sqlsc');";
$sql.="update cgs set gengxin='sqlgx' where id=1;";
$sql.="delete from cgs where id=3";
echo $sql."<br/>";
if($mysqli->multi_query($sql)){
echo "多条数据执行成功";
echo "<br/>最后插入的id是:".$mysqli->insert_id;
}
else{
echo "<br/>执行错误<br/>错误号:".$mysqli->errno."<br/>错误信息:".$mysqli->error;
}
$mysqli->close();
?>
Mysqli多条结果集的处理方式一:
<?php
$mysqli=new MySQLi("localhost","root","root","mysqlicn");
$sql="select * from cgs;";
$sql.="select * from cgs2;";
$sql.="desc cgs2";
echo $sql."<br/>";
if($mysqli->multi_query($sql)){
echo "执行成功<br/>";
$result=$mysqli->store_result();
//第一结果集处理
echo "<table align='center' border='1' width='600'>";
echo "<tr>";
while($filed=$result->fetch_field()){echo "<th>".$filed->name."</th>";}
echo "</tr>";
while($row=$result->fetch_assoc()){
echo "<tr>";
foreach($row as $col){
echo "<td>".$col."</td>";
}
echo "<tr/>";
}
echo "</table>";
//第二结果集处理
$mysqli->next_result();
$result=$mysqli->store_result();
echo "<table align='center' border='1' width='600'>";
echo "<tr>";
while($filed=$result->fetch_field()){echo "<th>".$filed->name."</th>";}
echo "</tr>";
while($row=$result->fetch_assoc()){
echo "<tr>";
foreach($row as $col){
echo "<td>".$col."</td>";
}
echo "<tr/>";
}
echo "</table>";
}
else{
echo "<br/>执行错误<br/>错误号:".$mysqli->errno."<br/>错误信息:".$mysqli->error;
}
$mysqli->close();
?>
Mysqli多条结果集的处理方式二(do while更智能):
<?php
$mysqli=new MySQLi("localhost","root","root","mysqlicn");
$sql="select * from cgs;";
$sql.="select * from cgs2;";
$sql.="desc cgs2";
echo $sql."<br/>";
if($mysqli->multi_query($sql)){
echo "执行成功<br/>";
do{
$result=$mysqli->store_result();
//第一结果集处理
echo "<table align='center' border='1' width='600'>";
echo "<tr>";
while($filed=$result->fetch_field()){echo "<th>".$filed->name."</th>";}
echo "</tr>";
while($row=$result->fetch_assoc()){
echo "<tr>";
foreach($row as $col){
echo "<td>".$col."</td>";
}
echo "<tr/>";
}
echo "</table>";
}
while($mysqli->next_result());//使用do while()每次先处理结果集,然后while自动判断是否存在下一个结果集,若存在就将指针指向下一个结果集然后执行下一个结果集
}
else{
echo "<br/>执行错误<br/>错误号:".$mysqli->errno."<br/>错误信息:".$mysqli->error;
}
$mysqli->close();
?>
Mysqli多条结果集处理方式三(结果集分隔):
<?php
$mysqli=new MySQLi("localhost","root","root","mysqlicn");
$sql="select * from cgs;";
$sql.="select * from cgs2;";
$sql.="desc cgs2";
echo $sql."<br/>";
if($mysqli->multi_query($sql)){
echo "执行成功<br/>";
do{
$result=$mysqli->store_result();
//第一结果集处理
echo "<table align='center' border='1' width='600'>";
echo "<tr>";
while($filed=$result->fetch_field()){echo "<th>".$filed->name."</th>";}
echo "</tr>";
while($row=$result->fetch_assoc()){
echo "<tr>";
foreach($row as $col){
echo "<td>".$col."</td>";
}
echo "<tr/>";
}
echo "</table>";
if($mysqli->more_results()){
echo "<hr/>";//判断当前结果集之后还有结果集,若存在就用hr标签分隔
}
}
while($mysqli->next_result());//使用do while()每次先处理结果集,然后while自动判断是否存在下一个结果集,若存在就将指针指向下一个结果集然后执行下一个结果集
}
else{
echo "<br/>执行错误<br/>错误号:".$mysqli->errno."<br/>错误信息:".$mysqli->error;
}
$mysqli->close();
?>
事务处理的定义:多个sql可看作一个事务,事务的任何一个环节出错都将会撤销当前整个事务的执行(回滚事务),如果整个环节都没有问题方能提交。
事务处理所在表引擎必须是innodb,myisam表引擎是不支持事务处理的。
创建表时指定表引擎:
create table zh(id int not null auto_increment primary key,name varchar(30),ye double) ENGINE='innodb';
语句回滚:
Rollback;
默认提交方式为自动提交,所以rollback回滚是无效的。事务的开启是需要关闭回滚的。
关闭自动提交与开启事务处理:
set autocommit=0;
start transaction;
事务确认无误手动提交:
commit;
完整的事务处理案例:
<?php
header("Content-type:text/html;charset=utf-8;");
$mysqli=new MySQLi("localhost","root","root","mysqlicn");
//事务处理
$mysqli->autocommit(0);
//关闭自动提交,若数据库中已经关闭自动提交此处也不会报错,但php的手动提交数据是会失败的,同样没有报错信息,但数据库看不到更改成功的数据。
$sql1="update zh set ye=ye-5 where name='a';";
$result=$mysqli->query($sql1);
$bs=true;
if($result){
echo "转出数据执行成功<br/>";
if($mysqli->affected_rows!=0){echo "从a转出成功<br/>";$bs=true;}
else{echo "从a转出失败<br/>";$bs=false;}
}
else{
echo "数据执行失败<br/>";
$bs=false;
}
$sql2="update zh set ye=ye+5 where name='b';";
$result2=$mysqli->query($sql2);
if($result2){
echo "转入数据执行成功<br/>";
if($mysqli->affected_rows!=0){echo "从b转入成功<br/>";$bs=true;}
else{echo "从b转入失败<br/>";$bs=false;}
}
else{
echo "转入数据执行失败<br/>";
$bs=false;
}
if($bs){
echo "从a转到b成功<br/>";
$mysqli->commit();
$mysqli->autocommit(1);
}
else{
echo "从a转到b失败<br/>";
$mysqli->rollback();
}
$mysqli->close();
?>
mysqli设置编码:
$mysqli=new MySQLi("localhost","root","root","mysqlicn");
$mysqli->set_charset("utf8");
关键字词:mysqli,多语句,事务处理