PHP操作MySQL事务实例,mysql事务实例_PHP教程

WBOY
Lepaskan: 2016-07-13 10:15:10
asal
947 orang telah melayarinya

PHP操作MySQL事务实例,mysql事务实例

本文实例讲述了PHP操作MySQL事务的方法,分享给大家供大家参考。具体方法如下:

一般来说,事务都应该具备ACID特征。所谓ACID是Atomic(原子性),Consistent(一致性),Isolated(隔离性),Durable(持续性)四个词的首字母所写,下面以“银行转帐”为例来分别说明一下它们的含义:

① 原子性:组成事务处理的语句形成了一个逻辑单元,不能只执行其中的一部分。换句话说,事务是不可分割的最小单元。比如:银行转帐过程中,必须同时从一个帐户减去转帐金额,并加到另一个帐户中,只改变一个帐户是不合理的。
② 一致性:在事务处理执行前后,数据库是一致的。也就是说,事务应该正确的转换系统状态。比如:银行转帐过程中,要么转帐金额从一个帐户转入另一个帐户,要么两个帐户都不变,没有其他的情况。
③ 隔离性:一个事务处理对另一个事务处理没有影响。就是说任何事务都不可能看到一个处在不完整状态下的事务。比如说,银行转帐过程中,在转帐事务没有提交之前,另一个转帐事务只能处于等待状态。
④ 持续性:事务处理的效果能够被永久保存下来。反过来说,事务应当能够承受所有的失败,包括服务器、进程、通信以及媒体失败等等。比如:银行转帐过程中,转帐后帐户的状态要能被保存下来。

在PHP中,mysqli 已经很好的封装了mysql事务的相关操作。如下示例:

复制代码 代码如下:
$sql1 = "update User set ScoreCount = ScoreCount +10 where ID= '123456'";
$sql2 = "update ScoreDetail  set FScore = 300 where ID= '123456'";
$sql3 = "insert into  ScoreDetail ID,Score) values ('123456',60)";
$mysqli = new mysqli('localhost','root','','DB_Lib2Test');
$mysqli->autocommit(false);//开始事物
$mysqli->query($sql1);
$mysqli->query($sql2);
if(!$mysqli->errno){
  $mysqli->commit();
  echo 'ok';
}else{
 echo 'err';
  $mysqli->rollback();
}

在这里,我们再使用 php mysql 系列函数执行事务。
复制代码 代码如下:
$sql1 = "update User set ScoreCount = ScoreCount +10 where ID= '123456'";
$sql2 = "update ScoreDetail  set FScore = 300 where ID= '123456'";
$sql3 = "insert into  ScoreDetail ID,Score) values ('123456',60)";
$conn = mysql_connect('localhost','root','');
mysql_select_db('DB_Lib2Test');
mysql_query('start transaction');
//mysql_query('SET autocommit=0');
mysql_query($sql1);
mysql_query($sql2);
if(mysql_errno ()){
    mysql_query('rollback');
    echo 'err';
}else{
    mysql_query('commit');
    echo 'ok';
}
// mysql_query('SET autocommit=1');
// mysql_query($sql3);

在这里要注意:

MyISAM:不支持事务,用于只读程序提高性能
InnoDB:支持ACID事务、行级锁、并发
Berkeley DB:支持事务

希望本文所述对大家的PHP+MySQL数据库程序设计有所帮助。

谁给个php操作mysql类并有详细使用说明或例子

下面这个,是针对php5的一个简单数据库封装类,适合学习,其他的如删除、更新等操作,你可以自己加上:
class Mysql{ //首先定义一个类,首写字母大写
public $host;//服务器名,访问修饰符PUBLIC证明$host是一个公共的属情在类的内部外部都可访问,可以被继承
public $user;//用户名,是公共的属性
private $pass;//密码,问修饰符private证明$pass是私有的.只能在类的内部使用且不能被继承.
public $dbname;//数据库名,也是公共的属性.
//__construct声名这是一个造函数,定义一些初始的信息.有三个参数
public function __construct($host,$user,$pass,$dbname){
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbname = $dbname;
$link = @mysql_connect($this->host,$this->user,$this->pass)
or die("error");
@mysql_select_db($this->dbname,$link)
or die("error2");
}
//定义数据库的查寻和显示函数
function myQuery($sql){
$result = mysql_query($sql);
if(!$result){
echo "error3";
exit;
}
$num = mysql_num_rows($result);
if($num){
echo "NO".$num;
}
while($row = mysql_fetch_assoc($result)){
echo '

'.htmlspecialchars(stripslashes($row['body']))."<pre class="brush:php;toolbar:false">";<br>         }<br>     }<br>}<br>$rutt = new Mysql('localhost','root','ssss','calvin');//实例化一个类...记住这里的参数是和构造函数的参数一样的...<br>$rutt->myQuery(......余下全文>><br>
  
<div class="header2"><span class="icon i-relatedanswer"><h3>想做个 网站 ,一段PHP编程代码,PHP的MYSQL缓存怎实现? 最好举个例子</h3></span></div><div class="best-replyer"></div>
<p class="ft">
//以下是缓存类:<br><?php <br />class cache {<br>   //缓存目录<br>   var $cacheRoot  = "./cache/";<br>   //缓存更新时间秒数,0为不缓存<br>   var $cacheLimitTime   = 0;<br>   //缓存文件名<br>   var $cacheFileName    = "";<br>   //缓存扩展名<br>   var $cacheFileExt     = "html";<br><br>   /*    <br>    * 构造函数    <br>    * int $cacheLimitTime 缓存更新时间    <br>    */     <br>   function cache( $cacheLimitTime ) {<br>     if( intval( $cacheLimitTime ) ) <br>$this->cacheLimitTime = $cacheLimitTime;<br>     $this->cacheFileName = $this->getCacheFileName();<br>//echo $this->cacheFileName;<br>     ob_start();<br>   }<br><br>   /*    <br>    * 检查缓存文件是否在设置更新时间之内    <br>    * 返回:如果在更新时间之内则返回文件内容,反之则返回失败    <br>    */     <br>   function cacheCheck(){<br>     if( file_exists( $this->cacheFileName ) ) {<br>$cTime = $this->getFileCreateTime( $this->cacheFileName );<br>if( $cTime + $this->cacheLimitTime > time() ) {<br>  echo file_get_contents( $this->cacheFileName );<br>  ob_end_flush();<br>  exit;<br>}<br>     }<br>     return false;<br>   }<br><br>    /*    <br>    * 缓存文件或者输出静态    <br>    * string $staticFileName 静态文件名(含相对路径)    <br>    */     <br>   function caching( $staticFileName = "" ){<br>     if( $this->cacheFileName ) {<br>$cacheContent = ob_get_contents();<br> //echo $cacheContent;<br>ob_end_flush();<br><br>if( $staticFileName ) {<br>$this->saveFile( $staticFileName, $cacheContent );<br>}<br><br>if( $this->cacheLimitTime )<br>  $this->saveFile( $this->cacheFileName, $cacheContent );<br> }<br>   }<br><br>   /*    <br>  ......余下全文>><br>
  </p>
<p align="left"></p><div style="display:none;">
<span id="url" itemprop="url">http://www.bkjia.com/PHPjc/906673.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/906673.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">PHP操作MySQL事务实例,mysql事务实例 本文实例讲述了PHP操作MySQL事务的方法,分享给大家供大家参考。具体方法如下: 一般来说,事务都应...</span>
</div>
<div class="art_confoot"></div>
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!