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

PHP uses Mysql transaction instance analysis, phpmysql transaction instance_PHP tutorial

WBOY
Release: 2016-07-13 10:19:19
Original
835 people have browsed it

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;>

Copy after login

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:
class Mysql{ //First Define a class, with the first letter capitalized
public $host;//server name, access modifier PUBLIC proves that $host is a public attribute, accessible both inside and outside the class, and can be inherited
public $ user;//Username is a public attribute
private $pass;//Password, ask the modifier private to prove that $pass is private. It can only be used inside the class and cannot be inherited.
public $ dbname;//Database name is also a public attribute.
//__construct declares this to be a constructor, defining some initial information. There are three parameters
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");
}
//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 '< ;td bgcolor="#fffddd">

'.htmlspecialchars(stripslashes($row['body']))."
";
}
}
}
$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. ...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template