This document describes how to connect php to the mysql database. It has a good reference value. let's take a look at it below. This article mainly introduces the methods and steps for connecting php to the mysql database. It has good reference value. let's take a look at it together with the small editor.
Connect to the database:
Use object-oriented methods;
1. create a mysqli object and create a connection object
2. prepare an SQL statement
3. execute the SQL statement, if it is a query statement. Returned results and objects
4. find data from the result set object;
Query property code:
Query ($ SQL); // determines whether the returned content exists. if ($ reslut) is successfully executed {// if yes, data is found from the result set object; $ attr = $ reslut-> fetch_all (); // returns the index array: $ attr = $ reslut-> fetch_row () // returns the associated array: tch_assoc (); // returned object: $ attr = $ reslut-> fetch_object (); // returns both indexes and associations: $ arrt = $ reslut-> fetch_array (); var_dump ($ attr);?>
The final return value is a set Object. for the sake of clarity, we use fetch_all (), fetch_row (); fetch_object ();
Add, delete, and modify statements:
Add a message:
// Create a connection object $ db = new MySQLi ("localhost", "root", "12345678", "heiheihei "); // prepare the SQL statement $ SQL = "insert into cou values ('3-111 ', '', '123 ')"; // execute the SQL statement $ r = $ db-> query ($ SQL); if ($ r) // if $ r is true {echo "executed successfully ";} else {echo "execution failed ";}
Change one message:
Query ($ SQL); if ($ r) {echo "execution successful" ;}else {echo "execution failed" ;}?>
Delete:
Query ($ SQL); if ($ r) {echo "execution successful" ;}else {echo "execution failed" ;}?>
The above is the details of using object-oriented php to connect to the mysql database. For more information, see other related articles in the first PHP community!