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.