PHP Object-oriented way to access the database

Source: Internet
Author: User
Tags sql injection sql injection attack

<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 Code

Registration 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 Code

PHP Object-oriented way to access the database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.