To connect to a database:use an object-oriented approach;1. Create a Mysqli object, create a Connection object2. Prepare an SQL statement3. Execute the SQL statement, if it is a query statement. Successful return of results and objects4. Find the data from the result set object;Query Property Code:
<?PHP//Create a Mysqli object, create a Connection object$db=NewMysqli ("localhost", "root", "12345678", "Heiheihei");//The parentheses fill in the IP address domain name, username, password, database name//prepare an SQL statement$sql= "SELECT * FROM Student";//executes the SQL statement, if it is a query statement. Successful return of results and objects$reslut=$db->query ($sql);//determine if the return has content, execute successfullyif($reslut){//If successful, the data is found from the result set object; $attr=$reslut-Fetch_all ();
//returns an indexed array: $attr = $reslut->fetch_row ()//Returns an associative array: Tch_assoc ();//return object: $attr = $reslut->fetch_object ();// Returns both indexed and associated: $ARRT = $reslut->fetch_array (); Var_dump($attr);?>
The last return value is a set object, we use Fetch_all (), Fetch_row () in order to see the results clearly. Fetch_object ();
Add or delete the statement: added a message:
//Connecting Objects$db=NewMysqli ("localhost", "root", "12345678", "Heiheihei");//Preparing SQL statements$sql= "INSERT into cou values (' 3-111 ', ' hehe ', ' 803 ')"; //Execute SQL statement$r=$db->query ($sql);if($r)//if "is Ture{ Echo"Successful Execution";}Else { Echo"Execution Failed";}
Changing a message:
<?PHP//Connecting Objects$db=NewMysqli ("localhost", "root", "12345678", "Heiheihei");//Preparing SQL statements$sql= "Update cou set cname= ' haha ' where tno= ' 803 '"; //Execute SQL statement$r=$db->query ($sql);if($r){ Echo"Successful Execution";}Else { Echo"Execution Failed";}?>
Delete:
<?PHP//Connecting Objects$db=NewMysqli ("localhost", "root", "12345678", "Heiheihei");//Preparing SQL statements$sql= "Delete from cou where tno= ' 803 '"; //Execute SQL statement$r=$db->query ($sql);if($r){ Echo"Successful Execution";}Else { Echo"Execution Failed";}?>
PHP connection MySQL Database