PHP uses Mysql transaction instance parsing and phpmysql transaction instance

Source: Internet
Author: User

PHP uses Mysql transaction instance parsing and phpmysql transaction instance

This example explains how to use MySQL in PHP and provides a detailed description with annotations. Share it with you for your reference.

The specific example is as follows:

<? Php // database connection $ conn = mysql_connect ('localhost', 'root', ''); mysql_select_db ('test', $ conn ); mysql_query ("set names gbk");/* tables that support transactions must be InnoDB-type. a transaction can only appear once: mysql_query ('start transaction '); // start the transaction mysql_query ('rollback'); // roll back the transaction mysql_query ('commit '); // submit the transaction. If multiple ROLLBACK transactions occur in a transaction, when a transaction is committed, all operations on the database are canceled before the first rollback and after the transaction starts. After the first rollback, all operations on the database remain valid until the transaction is committed, therefore, the rollback statement is generally placed only before the transaction statement is submitted. If a transaction does not have a commit statement, all the following operations on the database are executed at the start of the transaction (the execution method returns the right or wrong ), but it has no impact on the database, but in the lower part of the Execution When starting a TRANSACTION statement, the previous TRANSACTION will automatically submit */mysql_query ('start transaction'); $ isBad = 0; $ ins_testTable1 = "insert into testtable1 (NAME, age) VALUES ('first', 23) "; if (! Mysql_query ($ ins_testTable1) {$ isBad = 1;} // An error occurred while inserting the statement field NAME $ 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);?>

I hope the examples described in this article will help you learn the PHP + MySQL programming.


How to use MySQL transactions in PHP-PHP advanced Discussion

Yes, MyISAM is the fastest speed when executing 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 a table that stores financial data ), or when the INSERT and SELECT statements are staggered (such as online message bar or forum system), InnoDB should be used for temporary tables or views. MEMORY tables can be used,
 
Who gives a php to operate mysql class and has detailed instructions or examples?

The following is a simple database encapsulation class for php5. It is suitable for learning. You can add other operations such as deletion and update:
<? Php
Class Mysql {// define a class with uppercase letters at the beginning
Public $ host; // server name. The access modifier PUBLIC certifies that $ host is a public entity that can be accessed from inside and outside the class and can be inherited.
Public $ user; // user name, which is a public attribute
Private $ pass; // password. The modifier private proves that $ pass is private. It can only be used inside the class and cannot be inherited.
Public $ dbname; // database name, which is also a public attribute.
// _ Construct: This is a build function that defines 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 database query and display functions
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 '<tr> <td bgcolor = "# fffddd"> <pre> '.html specialchars (stripslashes ($ row ['body']). "<pre> </td> </tr> ";
}
}
}
$ Rutt = new Mysql ('localhost', 'root', 'sss', 'calvin '); // instantiate a class... remember that the parameters here are the same as those of the constructor...
$ Rutt-> myQuery (... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.