Mysqli的批量CRUD資料,mysqlicrud

來源:互聯網
上載者:User

Mysqli的批量CRUD資料,mysqlicrud

學會了使用了mysqli擴充庫之後,總體感覺比面向過程的方法更加合理化,物件導向是大勢所趨,所以絕不能僅僅只用那一套老的mysql庫了,也不利於php對mysql的操作,因為我們在實際的開發中,很多還是物件導向開發的,在這裡,就說一下mysqli之所以成為擴充庫自然有其理由,就大量操作的特性就大大的提高了代碼的執行效率。


還是和以往一樣,我喜歡把sql語句分為dql語句和dml語句,主要是根據返回結果來分的,前者是結果集(需要釋放),後者是布爾值。


下面是大量操作dml語句的代碼:

<?php/*require_once "mysqltool.class.php";$SqlHelper=new SqlTool();$sql="insert into words(enword,chword) values('classroom','教室')";$res=$SqlHelper->execute_dml($sql);//$res->free();*///這是mysqli批量增刪改資料$mysqli= new MySqli("localhost","root","toor","education");if($mysqli->connect_error){die("串連失敗:".$mysqli->connect_error);}echo "串連成功<br/>";/*//大量新增資料$sql_1.="insert into words (enword,chword) values('classroom444','教室');";$sql_1.="insert into words (enword,chword) values( 'classroom222','教室');";$sql_1.="insert into words (enword,chword) values('classroom333','教室');";$b=$mysqli->multi_query($sql_1);*///大量刪除資料/*$sql_2.="delete from words where id=14;";$sql_2.="delete from words where id=15;";$sql_2.="delete from words where id=16;";$sql_2.="delete from words where id=17;";$sql_2.="delete from words where id=18;";$sql_2.="delete from words where id=19;";$sql_2.="delete from words where id=20;";$sql_2.="delete from words where id=22;";*///也可以批量修改。。。。/*但是不建議和select語句共用*/$b=$mysqli->multi_query($sql_2);if(!$b){echo "添加失敗";}else{echo "添加成功".$mysqli->connect_error;}$mysqli->close();?>


下面是操作dql語句的代碼:相對於dml就有一點複雜了:

<?php$mysqli= new MySqli("localhost","root","toor","education");if($mysqli->connect_error){die("串連失敗:".$mysqli->connect_error);}echo "串連成功<br/>";<span style="white-space:pre"></span>$sql_3.="select * from words;";$sql_3.="select * from words_test1;";<span style="white-space:pre"></span>if($res=$mysqli->multi_query($sql_3)){<span style="white-space:pre"></span>do{$result=$mysqli->store_result();while($row=$result->fetch_row()){foreach($row as $key=>$val){echo "----".$val;}echo "<br/>";}$result->free();if(!$mysqli->more_results()){break;}echo "***********************************************";}while($mysqli->next_result());}$mysqli->close();?>

主要就是後者返回的是一個結果集的集合,需要一層一層的把它剝開看能夠看到東西。


本文僅限參考和csdn轉載,尊重原創,查看我的MyCodeDream個人部落格點擊開啟連結



相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.