PHP connection MySQL database and delete and change

Source: Internet
Author: User

PHP connection database is generally used in an object-oriented approach, you need to create an object, that is, build a Connection object, and then write the SQL statement, (add to delete), and finally execute the SQL statement

Where we used to create the connection object is mysqli is not case-sensitive, here is the use of mysqli, meaning that the extension of MySQL, both in a process-oriented way can also be object-oriented way to interact with the database, The only difference is that the function (object method) is called differently.

After you create the connection object, you need to see if the connection is successful with the IF statement test Mysqli_connect_error () and the connection is unsuccessful if False is displayed

<?php//first create an object, that is, make a Connection object $db=new mysqli ("IP address domain name", "User name", "Password", "database Name");//write SQL statement $sql= "INSERT into  info values (' P003 ', ' Zhang San ', ' 1 ', ' n001 ');//execute the SQL statement $result= $db->query ($sql);//You can tell $result  the $result return value is Ture or false/* if ($result) {    echo "executed successfully";} else{      echo "Execution failed";} */?>

The above is about the connection MySQL database in the modified DELETE statement, relatively simple, but also relatively single,

The following is about connecting to the MySQL database in the check SELECT statement, because the final return value is a set object, in order to facilitate a clear view of the results we use Fetch_all (), Fetch_row (); Fetch_object ();

Fetch_assoc (); More common functions such as Fetch_array () are described later, and the output is in a different way

<?php//Create object $db=new mysqli ("," "," "," ");//The method of judging whether the error is/*if (Mysqli_connect_errtor ()) {     echo" Connection failed ";     Exit;} or Mysqli_connect_error () die ("Connection Failed"): ""; *///write SQL statement $sql= "SELECT * from Info";//Execute SQL statement return result set object $result= $db->query ($sql);//reads data from the result set, returns the array $attr= $result->fetch_all ()//reads all data, returns the index two-dimensional array $attr= $result->fetch_array ()// Reads the data pointed to by the current pointer, returns an array $attr= the index association exists $result->FETCH_ASSOC ()//returns an associative array $attr= $result->fetch_row ()//Returns an indexed array $attr=$ Result->fetch_object ()  //Return object?>

PHP connection MySQL database and delete and change

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.