1. 程式人生 > >【PHP】 mysqli_autocommit() 函數

【PHP】 mysqli_autocommit() 函數

連接 操作 strong function lba row article 保護 sqli

//獲取每一篇文章的內容
function getPost($f_parent_id, $f_title, $f_username, $f_board_id,$f_post_time, $f_ip,$content){
    $Artical = array();

    $conn=db_connect();
    $conn->autocommit(FALSE);

    $sql="INSERT into t_article (f_parent_id, f_title, f_username, f_board_id,f_post_time, f_ip) values";
    $sql.
="(‘".$f_parent_id."‘,‘".$f_title."‘,‘".$f_username."‘,‘".$f_board_id."‘,‘".$f_post_time."‘,‘".$f_ip."‘)"; $rs1=$conn->query($sql); $ttt=mysqli_insert_id($conn); $sq4="SELECT * from t_postinfo where f_uname=‘".$f_username."‘"; $rs4=$conn->query($sq4); if($rs4 && $rs4->num_rows){ $t
=$rs4->fetch_object(); $post_reply=$f_parent_id ? $t->f_reply_times : $t->f_post_times; } $sq2="INSERT into t_article_content (f_id, f_content ) values (LAST_INSERT_ID(),‘".$content."‘)"; $rs2=$conn->query($sq2); if($f_parent_id){ $sq3="UPDATE t_article set f_has_child=1 where f_id=‘".$f_parent_id."‘"; $rs3
=$conn->query($sq3); $sq4="UPDATE t_postinfo set f_reply_times=$post_reply+1 where f_uname=‘".$f_username."‘"; $rs4=$conn->query($sq4); }else{ $sq4="UPDATE t_postinfo set f_post_times=$post_reply+1 where f_uname=‘".$f_username."‘"; $rs4=$conn->query($sq4); } if( ($rs1 && $rs2 && $f_parent_id && $rs3 && $rs4) || ($rs1 && $rs2 && !$f_parent_id && $rs4)){ $conn->commit(); $ret=$ttt; }else{ $conn->rollback(); $ret=true; } $conn->close(); return $ret; }

mysqli_autocommit() 函數開啟或關閉自動提交數據庫修改。

mysqli_commit() 函數,用於提交指定數據庫連接的當前事務。

mysqli_rollback() 函數,用於回滾當前事務。

http://www.runoob.com/php/func-mysqli-autocommit.html

事務:一系列要發生的連續的操作

事務安全:一種保護連續操作同時滿足的一種機制

事務意義:保證數據操作的完整性

事務的操作:自動事務(默認)、手動事務

手動事務:

1.開啟事務:告訴系統以下所有操作不要直接寫入數據表,先存放到事務日誌;

2.進行事務操作;

3.關閉事務:選擇性的將日誌文件中操作的結果保存到數據表;

a.提交事務:同步數據表(操作成功)commit

b.回滾事務:情況日誌表(操作失敗)rollback

前提:引擎是innodb

回滾點:在某個成功的操作完成後,後續的操作可能成功可能失敗,可以在當前成功的位置,設置回滾點,可供後續失敗操作返回的位置,而不是返回所有操作。

【PHP】 mysqli_autocommit() 函數