Use PHP to access the MySql database and explain how to add, delete, modify, and query instances.

Source: Internet
Author: User

Use PHP to access the MySql database and explain how to add, delete, modify, and query instances.

PHP accesses the MySql database

<? Php // create a connection object $ db = new MySQLi ("localhost", "root", "", "0710_test "); // write the SQL statement $ SQL = "select * from student"; // check whether the database is connected successfully. if the connection fails, the system returns "connection failed" and exits the program if (mysqli_connect_error ()) {die ("connection failed");} // executes the SQL statement and returns the result set object $ result = $ db-> query ($ SQL ); var_dump ($ result-> num_rows); // determines whether the result has any data. if ($ result-> num_rows) {echo "data";} // retrieves data (query statement) // $ arr = $ result-> fetch_all (); // obtain all data and use a two-dimensional array to exist. // $ arr = $ result-> fetch_all (MYSQLI_ASSOC ); // retrieving all data /$ Arr = $ result-> fetch_array (); // while cyclically traverses all data in the array while ($ arr = $ result-> fetch_array ()) {var_dump ($ arr);} // $ arr = $ result-> fetch_assoc (); // returns the associated array // $ arr = $ result-> fetch_object (); // member variable corresponding to the column name // $ arr = $ result-> fetch_row (); // returns the Index Array // var_dump ($ arr ); // add, delete, modify, and delete a statement // Add a data record (return true or false) $ SQL = "insert into student values ('20140901', 'wang mou', 'male ', '2017-7-1 ', '20170101') "; // delete a data record (Return Value: true or false) $ SQL =" delete from student w Here Sname = 'Li jun' "; $ r = $ db-> query ($ SQL); var_dump ($ r);?>

Add, delete, modify, and delete instances in the auto-increment list of databases

<? Php $ db = new MySQLi ("localhost", "root", "", "0710_info"); if (mysqli_connect_error () {die ("connection failed ");} $ SQL = "insert into work values (0, 'p005 ', '2017-01-01', '2017-10-1 ', 'san Shi Day', 'ministry of Education', 2 )"; if ($ db-> query ($ SQL) {echo "added successfully"; // id of the inserted data (commonly used) echo $ db-> insert_id ;} else {echo "failed to add" ;}?>

The above example describes how to use PHP to access the MySql database and how to add, delete, modify, and query instances, we also hope that you can support the customer's home.

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.