Connect to the database:
Use an object-oriented approach;
1. Create a mysqli object and create a connection object
2. Prepare a SQL statement
3. Execute SQL statement, if it is a query statement. Successfully returned result and object
4. Find data from the result set object;
Query attribute code:
<?php //Create a mysqli object and create a connection object$db=new MySQLi("localhost","root","12345678","heiheihei"); //The brackets are filled in the IP address domain name, user name, password, and database name //Prepare a SQL statement$sql = "select * from student"; //Execute SQL statement, if it is a query statement. Successfully returned result and object$reslut = $db->query($sql); //Display whether there is any content when returning, and the execution is successfulif($reslut) { //If successful, find the data from the result set object; $attr = $reslut->fetch_all(); //Return the index array: $attr = $reslut->fetch_row()//Return associative array: tch_assoc();//Return object: $attr = $reslut->fetch_object();//Return both index and association: $arrt = $reslut->fetch_array(); var_dump($attr); ?>
The final return value is a set object. In order to facilitate clear viewing of the results, we used fetch_all(), fetch_row(); fetch_object();
Add, delete and modify statements:
Add one more message:
//Create connection objects$db = new MySQLi("localhost","root","12345678","heiheihei"); //Prepare SQL statement$sql = "insert into cou values('3-111','hey-hey','803')"; //Execute SQL statement$r = $db ->query($sql); if($r)//If $r is ture{ echo "Execution successful"; } else { echo "Execution failed"; }
Change a message:
<?php //Create connection objects$db = new MySQLi("localhost","root","12345678","heiheihei"); //Prepare SQL statement$sql = "update cou set cname='Ha ha' where tno='803'"; //Execute SQL statement$r = $db ->query($sql); if($r) { echo "Execution successful"; } else { echo "Execution failed"; } ?>
delete:
<?php //Create connection objects$db = new MySQLi("localhost","root","12345678","heiheihei"); //Prepare SQL statement$sql = "delete from cou where tno='803'"; //Execute SQL statement$r = $db ->query($sql); if($r) { echo "Execution successful"; } else { echo "Execution failed"; } ?>
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!