MYSQLI preprocessing and transaction processing

Source: Internet
Author: User
Tags sql client

1

Application environment

Mysqli preprocessing function (used in large amount of data processing)

2

Steps

A) Mysqli connection database

$mysqli = new mysqli (' localhost ', ' root ', ' root ', ' Chuanzhi ');

b) Set the encoding

$mysqli->set_charset (' gb2312 ');

c) Send query statement

Using $mysqli->prepare ($query) This method means preprocessing, and if a value is involved, the query field must be specified

$query = "Select Id,proname,proclass,proprice from product where id>?";

$stmt = $mysqli->prepare ($query);

d) Provision of conditions

Like the last sentence? As an indeterminate value, you need to specify

$stmt->bind_param (' i ', $id); With variable bindings? The value represented, I denotes an integer, D is a float, B is binary, and s represents all other

$id = ' 1 '; Specifying the value of a variable

$stmt->execute (); Send a variable, the server statement is now complete

e) retrieving results from SQL Server

The preprocessing statement saves the result set in the SQL Open memory, and PHP's memory cannot be used directly (count is a PHP function, not an SQL function, cannot directly interact with SQL memory), and by default every fetch () takes a row out of SQL memory. However, you cannot get the number of rows without using Store_result (). Of course, if you have only one line of results, you can ignore this sentence. Keep in mind that fetching data in mysqli preprocessing can only be done with fetch () because the Mysqli_stmt class has no other value method

$stmt->store_result ();//If the variable is received, it will be found to be successful display 1, otherwise display 0

f) Get the number of result set rows

$count = $stmt->num_rows ();

g) fields to be output for binding result sets

$stmt->bind_result ($id, $proName, $proClass, $proPrice); The name does not matter, anyway, in order to match the query field, the number of variables to match the number of query fields

h) Output Results

As I said earlier, since we are going to take a value, of course, the preceding query specifies the queried field, the variable of the previous sentence is used

I

You know, close the mysqli link.

Transaction processing

1

Application environment

Use when you are concerned about safety

2

Steps

Only InnoDB and BDB types of data tables in MySQL can support transactional processing! Other types are not supported!

A) Connect the database and set the encoding

Needless to say, the connection database and the setup encoding are of course, MySQL and mysqli both support transaction processing

b) Turn off auto-commit

$mysqli->autocommit (FALSE); In SQL client, set autocommit = 0;

c) Open transaction

Open a transaction start transation (this sentence is only in MySQL, mysqli does not need this sentence);

d) Execute SQL statement

e) Determine if the result is what you want to do

If yes, submit $mysqli->commit ();

Otherwise, roll back the $mysqli->rollback ();

f) Turn on auto-commit

$mysqli->autocommit (TRUE); Personally, this sentence is not necessary, because the page executes, will return to the original state, that is, the auto-commit state

g) Close the database connection

$mysqli->close ();

MYSQLI preprocessing and transaction processing

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.