How to Use Mysqli to operate databases (connection, query, transaction rollback, etc.) in the PHP getting started tutorial.

Source: Internet
Author: User

How to Use Mysqli to operate databases (connection, query, transaction rollback, etc.) in the PHP getting started tutorial.

This example describes how to use Mysqli to operate databases in the PHP getting started tutorial. We will share this with you for your reference. The details are as follows:

Demo1.php

<? Php // use mysqli object to operate the database // create mysqli object (resource handle) $ _ mysqli = new mysqli (); // connect to database 1. host Name (ip) 2. account 3. password 4. database // mysqli_connect function === _ mysqli-> connect (); $ _ mysqli-> connect ('localhost', 'root', '123', 'Guest '); // disconnect MySQL mysqli_close () === _ mysqli-> close (); $ _ mysqli-> close ();?>

Demo2.php

<? Php // use the constructor $ _ mysqli = new mysqli ('localhost', 'root', '123', 'Guest ') without using connect '); // select a separate database // The Database Selected here will replace the above database // to avoid these troubles, try not to point to/$ _ mysqli-> select_db ('school '); $ _ mysqli-> close ();?>

Demo3.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); // connect to mysql // when an error occurs in your parameter, leading to a connection error, // $ _ mysqli is not successfully created, that is, the function without a resource handle // does not call methods and attributes in mysqli. @ $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'Guest '); // Why should I use a function to capture it? // Why not use the object-oriented method for capturing? If (mysqli_connect_errno () {echo 'database connection error. error message :'. mysqli_connect_error (); exit () ;}$ _ mysqli-> close () ;?>

Demo4.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); // connect to mysql // when an error occurs in your parameter, leading to a connection error, // $ _ mysqli is not successfully created, that is, the function without a resource handle // does not call methods and attributes in mysqli. @ $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'Guest '); // Why should I use a function to capture it? // Why not use the object-oriented method for capturing? If (mysqli_connect_errno () {echo 'database connection error. error message :'. mysqli_connect_error (); exit () ;}// $ _ mysqli-> select_db ('fsdf'); // if ($ _ mysqli-> errno) error during database operations) {echo 'database operation error :'. $ _ mysqli-> error; }$ _ mysqli-> close ();?>

Demo5.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo 'database connection error. error message :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create an SQL statement, obtain database table data $ _ SQL = "SELECT * FROM tg_user"; // execute an SQL statement, grant the result set $ _ result = $ _ mysqli-> query ($ _ SQL); // var_dump ($ _ re Sult); // object (mysqli_result) #2 (0) {}// through the result set, I want to obtain the first row of Data // fetch_row (); is the returned array, it contains the first data set print_r ($ _ result-> fetch_row (); // run once, And the pointer moves down a print_r ($ _ result-> fetch_row ()); // destroy the result set $ _ result-> free (); $ _ mysqli-> close ();?>

Demo6.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create an SQL statement, obtain database table data $ _ SQL = "SELECT * FROM tg_user"; // create a result set $ _ result = $ _ mysqli-> query ($ _ SQL ); // use the index array value // print_r ($ _ result-> fetch _ Row (); $ _ row =$ _ result-> fetch_row (); echo $ _ row [3]; // traverse, subscript is hard to remember [3] while (!! $ _ Row =$ _ result-> fetch_row () {echo $ _ row [3]. '<br/>'; }$ _ mysqli-> close ();?>

Demo7.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create an SQL statement, obtain database table data $ _ SQL = "SELECT * FROM tg_user"; // create a result set $ _ result = $ _ mysqli-> query ($ _ SQL ); // use the associated array value // print_r ($ _ result-> fetch _ Assoc (); $ _ assoc =$ _ result-> fetch_assoc (); echo $ _ assoc ['tg _ username']; // traverse while (!! $ _ Assoc =$ _ result-> fetch_assoc () {echo $ _ assoc ['tg _ username']. '<br/>'; }$ _ mysqli-> close ();?>

Demo8.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create an SQL statement, obtain database table data $ _ SQL = "SELECT * FROM tg_user"; // create a result set $ _ result = $ _ mysqli-> query ($ _ SQL ); // use index + join array value // print_r ($ _ result-> fet Ch_array (); $ _ array =$ _ result-> fetch_array (); echo $ _ array [3]; echo $ _ array ['tg _ username']; // traverse ..... $ _ mysqli-> close ();?>

Demo9.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create an SQL statement, obtain database table data $ _ SQL = "SELECT * FROM tg_user"; // create a result set $ _ result = $ _ mysqli-> query ($ _ SQL ); // use the OOP method object // print_r ($ _ result-> Fetch_object (); echo $ _ result-> fetch_object ()-> tg_username; // use OOP to traverse while (!! $ _ Object =$ _ result-> fetch_object () {echo $ _ object-> tg_username. '<br/>'; }$ _ mysqli-> close ();?>

Demo10.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create an SQL statement, obtain database table data $ _ SQL = "SELECT * FROM tg_user limit "; // create a result set $ _ result =$ _ mysqli-> query ($ _ SQL); // I want to see how many lines I have selected echo $ _ resul T-> num_rows; // how many rows have I affected? echo $ _ mysqli-> affected_rows; $ _ mysqli-> close ();?>

Demo11.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create an SQL statement, obtain database table data $ _ SQL = "UPDATE tg_user SET tg_username = 'all-in-one website creation 'WHERE tg_id = 5 "; // create a result set $ _ result =$ _ mysqli-> query ($ _ SQL); // I want to view Next, how many lines have I selected echo $ _ result-> num_rows; echo '|'; // how many lines have I affected? echo $ _ mysqli-> affected_rows; $ _ mysqli-> close ();?>

Demo12.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create an SQL statement, obtain database table data $ _ SQL = "SELECT * FROM tg_user"; // create a result set $ _ result = $ _ mysqli-> query ($ _ SQL ); // obtain the number of fields in the table echo $ _ result-> field_cou Nt; // obtain the field name // $ _ field =$ _ result-> fetch_field (); // echo $ _ field-> name; // $ _ field =$ _ result-> fetch_field (); // echo $ _ field-> name; // while (!! $ _ Field =$ _ result-> fetch_field () {// echo $ _ field-> name. '<br/>'; // obtain all fields $ _ fields =$ _ result-> fetch_fields () at a time (); // echo $ _ fields [0]-> name; foreach ($ _ fields as $ _ field) {echo $ _ field-> name. '<br/>'; }$ _ mysqli-> close ();?>

Demo13.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create an SQL statement, obtain database table data $ _ SQL = "SELECT * FROM tg_user"; // create a result set $ _ result = $ _ mysqli-> query ($ _ SQL ); // move the Data Pointer $ _ result-> data_seek (9); $ _ ro W = $ _ result-> fetch_row (); echo $ _ row [3]; // move the field pointer $ _ result-> field_seek (3 ); $ _ field =$ _ result-> fetch_field (); echo $ _ field-> name; $ _ mysqli-> close ();?>

Demo14.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create three modified SQL statements $ _ SQL. = "UPDATE tg_article SET tg_username = 'kakaka' WHERE tg_id = 1;"; $ _ SQL. = "UPDATE tg_flower SET tg_fromuser = 'kakaka' WHERE Tg_id = 1; "; $ _ SQL. = "UPDATE tg_friend SET tg_fromuser = 'callaca' WHERE tg_id = 1"; // use the notification execution method $ _ mysqli-> multi_query ($ _ SQL ); // The normal method that can only execute SQL is: $ _ mysqli-> query ($ _ SQL); $ _ mysqli-> close ();?>

Demo15.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // create three selection statements $ _ SQL. = "SELECT * FROM tg_photo;"; $ _ SQL. = "SELECT * FROM tg_user;"; $ _ SQL. = "SELECT * FROM tg_friend"; if ($ _ mysqli-> mult I _query ($ _ SQL) {// obtain the current result set $ _ result =$ _ mysqli-> store_result (); print_r ($ _ result-> fetch_row ()); echo '<br/>'; // move the result set pointer to the next $ _ mysqli-> next_result (); $ _ result = $ _ mysqli-> store_result (); if (! $ _ Result) {echo 'The second SQL statement has five! '; Exit ();} print_r ($ _ result-> fetch_row (); echo' <br/> '; $ _ mysqli-> next_result (); $ _ result = $ _ mysqli-> store_result (); if (! $ _ Result) {echo 'The third SQL statement has five! '; Exit ();} print_r ($ _ result-> fetch_row ();} else {echo 'the first SQL statement is incorrect'; exit ();} $ _ mysqli-> close ();?>

Demo16.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); $ _ mysqli = new mysqli ('localhost', 'root', '123 ', 'testguest '); // database connection error if (mysqli_connect_errno () {echo' database connection error. the error message is :'. mysqli_connect_error (); exit () ;}// set the encoding $ _ mysqli-> set_charset ('utf8'); // set to disable automatic submission (manual submission) $ _ mysqli-> autocommit (false); // create two SQL statements $ _ SQL. = "UPDATE tg_flower SET tg_flower = tg_flower-50 WHERE tg_id = 1;"; $ _ SQL. = "UPDATE tg_friend SET tg_state = tg_state + 50 WHERE tg_id = 1"; // execute multiple SQL statements // as long as both SQL statements succeed, manually submit it to the database // otherwise, roll back and cancel the previous valid operations. If ($ _ mysqli-> multi_query ($ _ SQL) {// Number of affected rows, to determine whether the SQL statement is successfully executed // If $ _ success is false, it indicates that the SQL statement has Wu, then the rollback is executed, otherwise, manually submit $ _ success =$ _ mysqli-> affected_rows = 1? True: false; // move down the pointer $ _ mysqli-> next_result (); $ _ success2 =$ _ mysqli-> affected_rows = 1? True: false; // if both are successful, if ($ _ success & $ _ success2) {// execute manual submission $ _ mysqli-> commit (); echo 'perfect submit';} else {// roll back, undo all previous operations $ _ mysqli-> rollback (); echo all operations to zero! ';}} Else {echo' the first SQL statement has an error! ';} // Enable automatic submission $ _ mysqli-> autocommit (true); $ _ mysqli-> close ();?>

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.