PHP connection MySQL Database

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//create an object first, creating a Connection object$db=NewMysqli ("IP address domain name", "username", "password", "Database name"));//Write SQL statements$sql= "INSERT into info values (' p003 ', ' Zhang San ', ' 1 ', ' n001 ')";//Execute SQL statement$result=$db->query ($sql);//can judge $result the $result return value of the deletion 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//Creating Objects$db=NewMysqli ("," "," "," "");//method of judging whether or not to make mistakes/*if (Mysqli_connect_errtor ()) {echo "Connection failed"; Exit;} or Mysqli_connect_error () die ("Connection Failed"): "";*///Write SQL statements$sql= "SELECT * FROM Info";//Execute SQL statement returns result set object$result=$db->query ($sql);//reading data from the result set, returning an array$attr=$result->fetch_all ()//reads all data, returns an indexed two-dimensional array$attr=$result->fetch_array ()//reads the data that the current pointer points to, and returns an array that exists for the index association$attr=$result-&GT;FETCH_ASSOC ()//returns an associative array$attr=$result->fetch_row ()//returns an indexed array$attr=$result->fetch_object ()//return Object?>

PHP connection MySQL Database

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.