Home  >  Article  >  Backend Development  >  PHP uses Mysql transaction instance analysis, phpmysql transaction instance_PHP tutorial

PHP uses Mysql transaction instance analysis, phpmysql transaction instance_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:19814browse

PHP uses Mysql transaction instance analysis, phpmysql transaction instance

The examples in this article explain the use of MySQL transactions by PHP, and are provided with comments for detailed explanations. Share it with everyone for your reference.

Specific examples are as follows:

<&#63;php
//数据库连接
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('test', $conn);
mysql_query("SET NAMES GBK");

/*
支持事务的表必须是InnoDB类型
一段事务中只能出现一次:
mysql_query('START TRANSACTION');//开始事务
mysql_query(' ROLLBACK ');//回滚事务
mysql_query('COMMIT');//提交事务

如果一段事务中出现多次回滚事务,则在,提交事务时只将第一次回滚前至开始事务后对数据库的所有操作取消,第一次回滚后至提交事务前所有对数据库操作仍将有效,所以一般将回滚语句仅放在提交事务语句前
如果一段事务无提交语句,则从开始事务时以下的所有对数据库操作虽执行(执行方法返回对错),但对数据库无影响,但是在执行下段开始事务语句时,前段事务自动提交
*/
mysql_query('START TRANSACTION');
$isBad = 0;

$ins_testTable1 = "INSERT INTO testtable1(NAME,age)VALUES('first',23)";
if(!mysql_query($ins_testTable1)){
  $isBad =1;
}
//插入语句字段名有错
$ins_testTable2 = "INSERT INTO testtable1(NAME,ages)VALUES('second','24')";
if(!mysql_query($ins_testTable2)){
  $isBad =1;
}
if($isBad == 1){
  echo $isBad;
  mysql_query('ROLLBACK ');
}
mysql_query('COMMIT');
mysql_close($conn);
&#63;>

I hope the examples described in this article will be helpful to everyone’s learning of PHP+MySQL programming.

How to use MySQL transactions in PHP - PHP advanced discussion

Yes, MyISAM is the fastest when performing a large number of INSERT or SELECT statements. If you need the full-text search function, you should also use MyISAM when the transaction is very important (such as tables storing financial data), or when INSERT and SELECT statements are interleaved. In certain situations (such as online message boards or forum systems), InnoDB should be used. For temporary tables or to implement views, you can use MEMORY tables,

Who can provide detailed usage instructions or examples for using PHP mysql class?

The following is a simple database encapsulation class for php5, suitable for learning. For other operations such as deletion and update, you can add it yourself:
a46f45233ed95ee8b50a7bc391ee4b07host = $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");
}
//Define the search and display function of the database
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 'a34de1251f0d9fe1e645927f19a896e8< ;td bgcolor="#fffddd">e03b848252eb9375d56be284e690e873'.htmlspecialchars(stripslashes($row['body']))."e03b848252eb9375d56be284e690e873b90dd5946f0946207856a8a37f441edffd273fcf5bcad3dfdad3c41bd81ad3e5";
}
}
}
$rutt = new Mysql('localhost','root','ssss','calvin'); //Instantiate a class... Remember the parameters here are The same as the parameters of the constructor...
$rutt->myQuery(...the rest of the text>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/875869.htmlTechArticlePHP uses Mysql transaction instance analysis, phpmysql transaction instance This article explains the instance of PHP using MySQL transaction, and has Comments provide details. Share it with everyone for your reference. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn