process-oriented approach
Musqli Expanding library Operations MySQL database steps:
1. Get the connection and select the database
// Grammar Mysqli_connect (host,username,password,dbname,port,socket); $conn=mysqli_connect("127.0.0.1", ' Root ', ' ', ' user '); if (! $conn { die ("link Failed"); }
2. Set the Operation code
// syntax: Mysqli_set_charset (Connection,charset);//Modify database connection character set to UTF8mysqli_set_charset ($conn, "Utf-8") ;
3. Sending SQL instructions (divided into DDL statements and DML statements, DQL statements, DTL statements)
- DDL statements: Data definition statements
- DML statements: Data manipulation statements (update ', insert, delete)
- DQL statement: (SELECT)
- DTL statement: Data transaction statement (rollback, commit)
Query data (DML statement): Returns a result set
//EnquiryStatement $sql= "SELECT * from User1"; //Execute SQL statement $res=Mysqli_query($conn,$sql); //\ Print returns results as a result set Var_dump($res); result set:Object(Mysqli_result)#2 (5) {//["Current_field"]=> int (0)//["Field_count"]=> int (5)//["lengths"]=> NULL//["Num_rows"]=> Int (6)//["type"]=> int (0)//} Echo"<br/>"; Echo"----".$res-num_rows;//take data from the result set while($obj=Mysqli_fetch_object($res)) { Var_dump($obj); foreach($obj as $key=$value) { Echo"$key------$value"; Echo"<br/>"; } Echo"<br/>"; }
Name of function |
Description |
Mysqli_fetch_assoc ($result) |
query to a data returned as an associative array [key]=>value |
Mysqli_fetch_row ($result) takes the next row of data from the $res result set |
query to a data returned as an indexed array [0]=>value |
mysqli_fetch_array ($result) |
query to a data returned in a mixed form of an indexed array and an associative array [key]=> Valuec/[0]=>value |
mysqli_fetch_object ($result) |
query to a data is returned as an object property object{[key]=>value} |
mysqli_fetch_array ($result) |
query to one The data is returned in a mixed form of an indexed array and an associative array |
mysqli_fetch_array ($result, Mysqli_both) |
a query to The data is returned in a mixed form of an indexed array and an associative array |
mysqli_fetch_array ($result, MYSQLI_ASSOC) |
a query to The data is returned in a mixed form of associative arrays |
mysqli_fetch_array ($result, mysqli_row) |
query to one The data is returned in a mixed form of an indexed array |
Delete and change (DML statement): Returns a Boolean value
$sql= "INSERT into user1 (name,password,email,age) VALUES (' Haha ', MD5 (' 123456 '), ' [email protected] ', 16);"; $res=Mysqli_query($conn,$sql) or die("Execution Failed"); Var_dump($res);//The return is a Boolean value if(!$res) { Echo"Delete Failed"; } EchoNumber of rows affected:.mysqli_affected_rows($conn); if(mysqli_affected_rows($conn) >0){ Echo"Add Success"; }Else{ Echo"Number of rows not affected"; }
4. Releasing Resources
Note: Releasing the result set and closing the connection is not the same as filling in the resources
// releasing the result set Mysqli_free_result ($res); // function to close a previously opened database connection Mysqli_close ($conn);
Object-oriented approach
sqltool.class.php
<?PHPclasssqltool{Private $conn; Private $host= "localhost"; Private $user= ' Root '; Private $password= ' '; Private $db= ' user '; //construction method, initializing the connection database and setting character encoding function__construct () {$this->conn=Mysqli_connect($this->host,$this->user,$this->password,$this-db); if(!$this-conn) { die("Connection failed.")Mysqli_error()); } mysqli_set_charset ($this->conn, "UTF8"); } //executes a DML statement and returns a value functionEXCUTE_DML ($sql){ $res=Mysqli_query($this->conn,$sql) or die("Shiai").Mysqli_error(); if(!$res) { return0; }Else{ //mysqli_affected_rows: And the number of rows affected if(mysqli_affected_rows($this->conn) >0) { return1; }Else{ return2; } } } //executes the DQL statement and returns the value functionEXCUTE_DQL ($sql){ $res=Mysqli_query($this->conn,$sql) or die("Shibai"); //Var_dump ($res); return $res; } }
testsql.php
<?PHPrequire"Sqltool.class.php"; //$sql = "INSERT INTO User1 (name,password,email,age) VALUES (' Hhhhhh ', MD5 (' 123 '), ' [email protected] ',"); $sql= "Delete from User1 where name= ' QQ '"; $sqltool=NewSqltool (); //invoke DML $res=$sqltool->EXCUTE_DML ($sql); if($res==0) { Echo"Execution Failure <br/>"; }Else if($res==1) { Echo"Execution Success <br/>"; }Else if($res==2) { Echo"No impact to line count <br/>"; } $sql 1= "SELECT * from User1"; //Call DQL $res 1=$sqltool->EXCUTE_DQL ($sql 1); //Var_dump ($res 1); if($res 1->num_rows>0) { while($row=Mysqli_fetch_row($res 1)) { //Var_dump ($row); echo "<br/>"; foreach($row as $key=$value) { Echo"---$value"; } Echo"<br/>"; } }?>
"20" mysqli based on process-oriented and object-oriented programming