Building MYDBDA Database Encapsulation class
mydbda.php
<?PHPclassMydbda {var $host= "localhost"; var $username= "Root"; var $password= "123"; var $database= "MyDB"; /** Function: Execute SQL statement, return result parameter: $sql: SQL statement to execute $type: type of SQL statement, CX for query, QT for other $data: The database return value to manipulate: if it is a query, returns the result set if it is another statement, the execution returns OK successfully, the failure returns no*/ functionSelect ($sql,$type,$data) { //1. Making Connection Objects $db=NewMysqli ($this->host,$this->username,$this->password,$data); //2. Determine if the connection is successful if(Mysqli_connect_error()) { Echo"Connection Failed"; //exit the entire program Exit; } Else { //4. Execute SQL statements $result=$db->query ($sql); if($type= = "CX") { $str= ""; while($row=$result-Fetch_row ()) { for($i= 0;$i<Count($row);$i++) { $str=$str.$row[$i]." ^"; } $str=substr($str, 0,strlen($str)-1); $str=$str."|"; } $str=substr($str, 0,strlen($str)-1); return $str; } Else { if($result) { return"OK"; } Else { return"NO"; } } } } }?>
Processing Interface Source Code
chuli.php
<?PHPinclude("mydbda.php");/*$uid = $_post["UID"]; $pwd = $_post["pwd"];//1. Connect object $db = new mysqli ("localhost", "root", "123", "MyDB"); 2. Determine if the connection succeeded if (Mysqli_connect_error ()) {echo "Connection failed"; Exit the entire program exit;} else{$sql = "SELECT count (*) from login where Username= '". $uid. "' and password= '". $pwd. "'"; $result = $db->query ($sql); $row = $result->fetch_row (); if ($row [0]==0) {echo "Username or password error"; } else {echo "OK"; }}*/$uid=$_post["UID"];$db=NewMydbda ();$sql= "SELECT count (*) from login where Username= '".$uid."‘";$str=$db->select ($sql, "CX", "MyDB");Echo $str;?>
Main interface Source code
testajax.php
<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >User name:<input type= "text" id= "Uidz"/><div style= "color:red" id= "xs" ></div></body><script type= " Text/javascript ">$ (document). Ready (function(e) {$ ("#uidz"). Blur (function(){ varUID = $ (this).Val (); $.Ajax ({URL: "Chuli.php",Data: {Uid:uid},DataType: "TEXT",type: "POST",Success:function(data) {if(data== "1") { $("#xs"). Text ("The user name already exists!") "); } Else { $("#xs"). Text (""); } } }); }) $("#btn"). Click (function(){ varUID = $ ("#uid").Val (); varPWD = $ ("#pwd").Val (); $.Ajax ({URL: "Chuli.php",Data: {uid:uid,pwd:pwd},type: "POST",DataType: "TEXT",Success:function(data) {if(data = = "OK") {window. location = "index.php"; } Else{alert (data); } } }); }) });</script>Landing Interface Ajax Instance application