The Introduction to PHP Tutorial uses mysqli to manipulate database methods (connection, query, transaction rollback, etc.) _php tips

Source: Internet
Author: User
Tags php tutorial rollback first row

The example in this article describes the approach to using mysqli to manipulate databases in the introductory PHP tutorial. Share to everyone for your reference, specific as follows:

demo1.php

<?php
  //Use Mysqli object to manipulate database
  //Create Mysqli object (resource handle)
  $_mysqli = new mysqli ();
  Connection database 1. Host name (IP) 2. Account 3 password 4. Database
  //mysqli_connect function = = $_mysqli-> connect ();
  $_mysqli-> Connect (' localhost ', ' root ', ' 123456 ', ' guest ');
  Disconnect MySQL mysqli_close () = = $_mysqli-> close ();
  $_mysqli-> Close ();
? >

demo2.php

<?php
  //without connect, use the construction method directly
  $_mysqli = new mysqli (' localhost ', ' root ', ' 123456 ', ' guest ');
  Select a single database
  //The database selected here will replace the above database
  //To avoid these problems, as far as possible without going to point to the
  //$_mysqli-> select_db (' School ');
  $_mysqli-> Close ();
? >

demo3.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Connect MySQL
  //When you have an error in the parameter, causing the connection error, the
  //$_mysqli object is not created successfully, that is, the function without the resource handle
  //is the ability to invoke the methods and properties under Mysqli.
  @$_mysqli = new mysqli (' localhost ', ' root ', ' 123456 ', ' guest ');
  Why use a function to capture it?
  Why not use object-oriented approach to capture it?
  An error occurred in the IF (Mysqli_connect_errno ()) {
    echo ' database connection with the wrong message: '. Mysqli_connect_error ()
    ; Exit ();
  }
  $_mysqli->close ();
? >

demo4.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  Connect MySQL
  //When you have an error in the parameter, causing the connection error, the
  //$_mysqli object is not created successfully, that is, the function without the resource handle
  //is the ability to invoke the methods and properties under Mysqli.
  @$_mysqli = new mysqli (' localhost ', ' root ', ' 123456 ', ' guest ');
  Why use a function to capture it?
  Why not use object-oriented approach to capture it?
  An error occurred in the IF (Mysqli_connect_errno ()) {
    echo ' database connection with the wrong message: '. Mysqli_connect_error ()
    ; Exit ();
  }
  $_mysqli-> select_db (' FSDFD ');
  Error occurred in database operation
  if ($_mysqli-> errno) {
    echo ' database operation error: '. $_mysqli-> error;
  }
  $_mysqli->close ();
? >

demo5.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  $_mysqli = new mysqli (' localhost ', ' root ', ' 123456 ', ' testguest ');
  Error occurred during database connection
  if (Mysqli_connect_errno ()) {
    echo ' database connection error occurred, the error message is: '. Mysqli_connect_error ();
    Exit ();
  }
  Set the code
  $_mysqli-> set_charset (' UTF8 ');
  Create a SQL that gets the data for the table of the database
  $_sql = "SELECT * from Tg_user";
  Executes the SQL statement and assigns the result set to $_result
  $_result = $_mysqli-> query ($_sql);
  Var_dump ($_result); Object (Mysqli_result) #2 (0) {}
  //Through the result set, I want to get the first row of data
  //fetch_row (), an array to return, and a collection of the first Data
  Print_r ($_ Result-> Fetch_row ());
  Run once, the pointer moves down one
  print_r ($_result-> fetch_row ());
  Destroy result set
  $_result-> free ();
  $_mysqli->close ();
? >

demo6.php

<?php
  header (' content-type:text/html; charset=utf-8; ');
  $_mysqli = new mysqli (' localhost ', ' root ', ' 123456 ', ' testguest ');
  Error occurred during database connection
  if (Mysqli_connect_errno ()) {
    echo ' database connection has occurred. The wrong message is: '. Mysqli_connect_error ();
    Exit ();
  }
  Set the encoding
  $_mysqli->set_charset (' UTF8 ');
  Create a SQL that gets the data for the table of the database
  $_sql = "SELECT * from Tg_user";
  Create a result set
  $_result = $_mysqli->query ($_sql);
  Use an indexed array to
  //print_r ($_result->fetch_row ());
  $_row = $_result->fetch_row ();
  Echo $_row[3];
  Traversal, subscript 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 ', ' 123456 ', ' testguest ');
  Error occurred during database connection
  if (Mysqli_connect_errno ()) {
    echo ' database connection has occurred. The wrong message is: '. Mysqli_connect_error ();
    Exit ();
  }
  Set the encoding
  $_mysqli->set_charset (' UTF8 ');
  Create a SQL that gets the data for the table of the database
  $_sql = "SELECT * from Tg_user";
  Create a result set
  $_result = $_mysqli->query ($_sql);
  Use associative array to 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 ', ' 123456 ', ' testguest ');
  Error occurred during database connection
  if (Mysqli_connect_errno ()) {
    echo ' database connection has occurred. The wrong message is: '. Mysqli_connect_error ();
    Exit ();
  }
  Set the encoding
  $_mysqli->set_charset (' UTF8 ');
  Create a SQL that gets the data for the table of the database
  $_sql = "SELECT * from Tg_user";
  Create a result set
  $_result = $_mysqli->query ($_sql);
  Use index + associative array to fetch value
  //print_r ($_result->fetch_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 ', ' 123456 ', ' testguest ');
  Error occurred during database connection
  if (Mysqli_connect_errno ()) {
    echo ' database connection has occurred. The wrong message is: '. Mysqli_connect_error ();
    Exit ();
  }
  Set the encoding
  $_mysqli->set_charset (' UTF8 ');
  Create a SQL that gets the data for the table of the database
  $_sql = "SELECT * from Tg_user";
  Create a result set
  $_result = $_mysqli->query ($_sql);
  Using 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 ', ' 123456 ', ' testguest ');
  Error occurred during database connection
  if (Mysqli_connect_errno ()) {
    echo ' database connection has occurred. The wrong message is: '. Mysqli_connect_error ();
    Exit ();
  }
  Set the encoding
  $_mysqli->set_charset (' UTF8 ');
  Create a SQL that gets the data for the table of the database
  $_sql = "SELECT * from Tg_user limit 0,10";
  Create a result set
  $_result = $_mysqli->query ($_sql);
  I want to see how many lines I've chosen.
  Echo $_result->num_rows;
  How many lines 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 ', ' 123456 ', ' testguest ');
  Error occurred during database connection
  if (Mysqli_connect_errno ()) {
    echo ' database connection has occurred. The wrong message is: '. Mysqli_connect_error ();
    Exit ();
  }
  Set the encoding
  $_mysqli->set_charset (' UTF8 ');
  Create a SQL, get the database table of data
  $_sql = "UPDATE tg_user SET tg_username= ' one-stop building site ' WHERE tg_id=5 ';
  " Create a result set
  $_result = $_mysqli->query ($_sql);
  I want to see how many lines I've chosen.
  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 ', ' 123456 ', ' testguest ');
  Error occurred during database connection
  if (Mysqli_connect_errno ()) {
    echo ' database connection has occurred. The wrong message is: '. Mysqli_connect_error ();
    Exit ();
  }
  Set the encoding
  $_mysqli->set_charset (' UTF8 ');
  Create a SQL that gets the data for the table of the database
  $_sql = "SELECT * from Tg_user";
  Create a result set
  $_result = $_mysqli->query ($_sql);
  Find out how many fields are in the table
  echo $_result->field_count;
  Gets the name of the field
//$_field = $_result->fetch_field ();
Echo $_field->name;
$_field = $_result->fetch_field ();
Echo $_field->name;
//while (!! $_field = $_result->fetch_field ()) {
//   echo $_field->name. ' <br/> ';
  //Get all the fields once
  $_fields = $_result->fetch_fields ();
  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 ', ' 123456 ', ' testguest ');
  Error occurred during database connection
  if (Mysqli_connect_errno ()) {
    echo ' database connection has occurred. The wrong message is: '. Mysqli_connect_error ();
    Exit ();
  }
  Set the encoding
  $_mysqli->set_charset (' UTF8 ');
  Create a SQL that gets the data for the table of the database
  $_sql = "SELECT * from Tg_user";
  Create a result set
  $_result = $_mysqli->query ($_sql);
  Moving data pointer
  $_result->data_seek (9);
  $_row = $_result->fetch_row ();
  Echo $_row[3];
  Move 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 ', ' 123456 ', ' testguest ');
  Error occurred during database connection
  if (Mysqli_connect_errno ()) {
    echo ' database connection has occurred. The wrong 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= ' in Kerala ' WHERE tg_id=1;";
  $_sql. = "UPDATE tg_flower SET tg_fromuser= ' in Kerala ' WHERE tg_id=1;";
  $_sql. = "UPDATE tg_friend SET tg_fromuser= ' ka ' WHERE tg_id=1";
  Methods of using Notification execution
  $_mysqli->multi_query ($_sql);
  Common only way to execute SQL is: $_mysqli->query ($_sql);
  $_mysqli->close ();
? >

demo15.php

<?php header (' content-type:text/html; charset=utf-8; ');
  $_mysqli = new mysqli (' localhost ', ' root ', ' 123456 ', ' testguest ');
    Error occurred during database connection if (Mysqli_connect_errno ()) {echo ' database connection has occurred. The wrong message is: '. Mysqli_connect_error ();
  Exit ();
  //Set up the Code $_mysqli->set_charset (' UTF8 ');
  Create three SELECT statements $_sql. = "SELECT * from Tg_photo;";
  $_sql. = "SELECT * from Tg_user;";
  $_sql. = "SELECT * from Tg_friend";
    if ($_mysqli->multi_query ($_sql)) {//Get the current result set $_result = $_mysqli->store_result ();
    Print_r ($_result->fetch_row ());
    echo ' <br/> ';
    Moves the pointer of the result set to the next $_mysqli->next_result ();
    $_result = $_mysqli->store_result ();
      if (!$_result) {echo ' second SQL statement has five! ';
    Exit ();
    } print_r ($_result->fetch_row ());
    echo ' <br/> ';
    $_mysqli->next_result ();
    $_result = $_mysqli->store_result ();
      if (!$_result) {echo ' third SQL statement has five! ';
    Exit ();
  } print_r ($_result->fetch_row ()); } ElSe {echo ' first SQL statement is incorrect ';
  Exit ();

 } $_mysqli->close ();?>

Demo16.php

<?php header (' content-type:text/html; charset=utf-8; ');
  $_mysqli = new mysqli (' localhost ', ' root ', ' 123456 ', ' testguest ');
    Error occurred during database connection if (Mysqli_connect_errno ()) {echo ' database connection has occurred. The wrong message is: '. Mysqli_connect_error ();
  Exit ();
  //Set up the Code $_mysqli->set_charset (' UTF8 ');
  Set to turn off Autocommit (manually submitted) $_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 the two SQL statements are successful, submit them manually to the database or else, roll back, and undo the previous valid actions. if ($_mysqli->multi_query ($_sql)) {//By the number of rows affected, to determine whether the SQL statement was executed successfully///if $_success is false to indicate that the SQL statement has Wu, then perform a rollback or manually submit $ _success = $_mysqli->affected_rows = = 1?
    True:false;
    Move the pointer down $_mysqli->next_result (); $_success2 = $_mysqli->affected_rows = = 1?
    True:false;
      If both are successful if ($_success && $_success2) {//Execute manual submit $_mysqli->commit ();
    Echo ' perfect submission ';
   } else {   Perform a rollback, undo all previous Operations $_mysqli->rollback (); Echo ' All operations zeroing!
    ';
  } else {echo ' first SQL statement has errors! ';
  //Turn on autocommit $_mysqli->autocommit (true) again;

 $_mysqli->close ();?>

More interested in PHP related content readers can view the site topics: "PHP+MYSQLI Database Programming Skills Summary", "PHP object-oriented Program Design Introductory Course", "PHP Array" operation Skills Encyclopedia, "PHP Basic Grammar Introductory Course", " Summary of PHP operations and operator usage, "Summary of PHP Network programming skills", "PHP string (String) Usage Summary", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Related Article

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.