PHP使用Mysql事务实例解析,phpmysql事务实例
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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).
