久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php事務(wù)什么時候用

      php事務(wù)什么時候用

      在某些程序在執(zhí)行的時候需要進行多個動作,而我們的業(yè)務(wù)要求是某個動作在執(zhí)行錯誤的時候該進程所有的動作都不再執(zhí)行,全部執(zhí)行成功才算成功,否則就回到執(zhí)行之前的狀態(tài),這就需要用到事務(wù)的處理。

      原生:

      <?php 	$link = mysqli_connect('localhost','username','password');					//創(chuàng)建鏈接  	if(!$link) exit('數(shù)據(jù)庫連接失敗');											//判斷是否鏈接成功  	mysqli_set_charset($link , 'utf8');											//設(shè)置字符集 	 	mysqli_select_db($link,'myDatabase');										//選擇數(shù)據(jù)庫  	$sql1 = "正確的插入語句";													//準(zhǔn)備sql語句 	$sql2 = "錯誤的插入語句";													  	$result1 = mysqli_query($link , $sql1);										//發(fā)送sql語句 	$result2 = mysqli_query($link , $sql2);	  	if($result1 && $result2) { 		mysql_query(“COMMIT”);													//提交事務(wù) 		echo "提交成功"; 	} else { 		mysql_query("ROLLBACK"); 		echo '數(shù)據(jù)回滾'; 	}  	mysql_query("END");															//事務(wù)結(jié)束	

      PDO:

      try {  	 //實例化PDO對象 	 $pdo = new PDO("mysql:host=localhost;dbname=test","root","root",array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));    	 $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 	 	 //開啟事務(wù) 	 $pdo->beginTransaction(); 	   	 //執(zhí)行sql語句 	 $pdo->exec("insert into t1(username,password,rtime,rip) values('shiwu2','shiwu2','456456456','456456456')");      $pdo->exec("insert into t2(username,password,rtime,rip) values('shiwu2',shiwu2','456456456','456456456')");            //提交事務(wù)      $pdo->commit();      //PDO   PDOStatement  PDOException } catch(PDOException $e) { 	 //回滾事務(wù)      $pdo->rollBack(); 	 echo "數(shù)據(jù)回滾";     }

      TP5框架:

      //模型方法 function demo{ 	//開啟事務(wù) 	$this->startTrans(); 	 	//業(yè)務(wù)邏輯操作 	$data['id'] = 1; 	$res = $this->insertUserInfo($data);		//保存用戶信息 	 	if($res) { 		//提交事務(wù) 		$this->commit(); 		return $res; 	} else { 		//事務(wù)回滾 		$this->rollback(); 	} }

      以上內(nèi)容僅供參考!

      推薦教程:PHP視頻教程

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號