PHP使用Mysql事务实例解析,phpmysql事务实例
本文实例讲解了PHP使用MySQL事物的实例,并备有注释加以详细说明。分享给大家供大家参考之用。
具体实例如下所示:
<?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); ?>
希望本文所述实例对大家PHP+MySQL程序设计的学习有所帮助。
对呀,进行大量的INSERT或SELECT语句时MyISAM速度最快如果需要全文搜索功能,也应该使用MyISAM当事务非常重要(例如存储财务数据的表格),或在INSERT和SELECT语句是交错执行的情况下(例如在线的消息栏或论坛系统)时应该用InnoDB对于临时表格或者要实现视图,可以使用MEMORY表格,
下面这个,是针对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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.