<body><?PHP//Object-oriented access to the database//Build Objects$db=NewMysqli ("localhost", "root", "123", "MyDB");//determine if the connection is wrong/*if (Mysqli_connect_error ()) {echo ' Connection failed! "; Exit;}*/!Mysqli_connect_error() or die("Connection Failed! ");//Write SQL statements$sql= "INSERT into Info values (' p010 ', ' Zhang San ', true, ' n002 ', ' 1988-2-3 ')";//Execute SQL statement, query statement returns result set object, other statements return TRUE or False$result=$db->query ($sql);//reads data from the result set object//$attr = $result->fetch_all (mysqli_both);//returns all data as a two-dimensional array//$attr = $result->fetch_array (); Returns the data that the current pointer points to//$attr = $result->fetch_assoc (); Returns the data that the current pointer points to (associative array)//$attr = $result->fetch_object (); Return object//$attr = $result->fetch_row (); Returns an indexed array of//var_dump ($attr);/*while ($attr = $result->fetch_row ()) {var_dump ($attr);}*///Var_dump ($result);?></body>
View Code
Example
Login interface
<body>
View Code<?PHP$uid=$_post["UID"];$pwd=$_post["PWD"];//created Objects$db=NewMysqli ("localhost", "root", "123", "MyDB");//Judging if there is an error!Mysqli_connect_error() or die("Connection Failed! ");//Write SQL statement//easy to SQL injection attack, unsafe//$sql = "SELECT count (*) from Users where Uid = ' {$uid} ' and pwd= ' {$pwd} '";$sql= "Select Pwd from Users where uid= ' {$uid}‘";//Execute SQL statement$result=$db->query ($sql);//Fetch Data$attr=$result-Fetch_row ();if($attr[0] = =$pwd){ Header("location:main.php");}Else{ Echo"Login failed!" ";
View CodeRegistration interface
<body><form action= "zcchuli.php" method= "POST" ><div> User name: <input type= "text" name= "UID"/> </div><div> Password: <input type= "text" name= "pwd"/></div><div> name: <input type= "Text" Name= "Name"/></div><div> Gender: <input type= "Radio" value= "1" name= "sex"/> Male <input type= "Radio" Value= "0" name= "sex"/> Women </div><div> Birthdays: <input type= "text" name= "Birthday"/></div>< Input type= "submit" value= "register"/></form></body>
View Code<?PHP$uid=$_post["UID"];$pwd=$_post["PWD"];$name=$_post["Name"];$sex=$_post["Sex"];$birthday=$_post["Birthday"];$s=$sex==1?true:false;//created Objects$db=NewMysqli ("localhost", "root", "123", "MyDB");//Judging if there is an error!Mysqli_connect_error() or die("Connection Failed! ");//Write SQL statements$sql= "INSERT into Users values (' {$uid}‘,‘{$pwd}‘,‘{$name}‘,{$s},‘{$birthday} ', 0) ';//Execute SQL statement$result=$db->query ($sql);if($result){ Header("location:zhuce.php");}Else{ Echo"Registration failed!" ";}
View CodePHP Object-oriented way to access the database