New Encapsulation Class
<?phpclass dbda{Public $host= "localhost";//Server AddressPublic $uid = "root";//User namePublic $pwd = "";//PasswordPublic $conn;//Connection Object //ways to manipulate databases //$sql represents the SQL statement that needs to be executed //$type represents the type of SQL statement, 1 represents the query, 0 represents //$db represents the name of the database to manipulate //returns a two-dimensional array if it is a query //returns TRUE or False if it is a different statement function__construct ($db = "Bilibili") { //Connecting Objects$ This->conn =NewMysqli ($ This->host,$ This->uid,$ This-pwd, $db); } PublicfunctionQuery ($sql, $type =1) { //Judging if there is an error!mysqli_connect_error () or Die ("Connection failed! "); //Execute SQL statement$result = $ This->conn->query ($sql); //determine the type of SQL statement if($type ==1) { //if it is a query statement, returns a two-dimensional array of result sets return$resultFetch_all (); } Else { //returns TRUE or False if it is a different statement return$result; } } //Ajax calls return JSONPublicfunctionJsonquery ($sql, $type =1, $db = "Bilibili") { //defining a data source$DSN = "mysql:dbname={$db};host={$this->host}"; //Making PDO Objects$pdo =NewPDO ($dsn, "{$this->uid}", "{$this->pwd}"); //prepare to execute SQL statement$st = $pdoprepare ($sql); //executing a preprocessed SQL statement if($stExecute ()) { if($type ==1) {$sttr= $stFetchall (PDO::FETCH_ASSOC); returnJson_encode ($attr); } Else { if($st) {return"OK"; } Else { return"NO"; } } } Else{echo"Execution failed!" "; } } //Ajax Call return stringPublicfunctionStrquery ($sql, $type =1) { //determine if the connection is successful!mysqli_connect_error () or Die ("Connection failed! "); //Execute SQL statement$result = $ This->conn->query ($sql); //determine the type of SQL statement if($type ==1) {$attr= $resultFetch_all (); $str= ""; //If the query statement returns a string for($i =0; $i <count ($attr); $i + +) { for($j =0; $j <count ($attr [$i]); $j + +) {$str=$str. $attr [$i] [$j]; $str= $str. " ^"; } $str= substr ($str, 0,strlen ($STR)-1); $str= $str. "|"; } $str= substr ($str, 0,strlen ($STR)-1); return$str; } Else { //returns TRUE or False if it is a different statement if($result) {return"OK"; } Else { return"NO"; } } } functionPdoquery ($sql, $type =1, $db = "Bilibili") { //Creating data Sources$dns = "mysql:host={$this->host};d bname={$db}"; //Making PDO Objects$pdo =NewPDO ($dns, $ This->uid,$ This-pwd); //prepare an SQL statement$stm = $pdoprepare ($sql); //executing a preprocessing statement$r = $stmexecute (); if($r) {if($type ==1) { return$stmFetchall (); } Else { return"OK"; } } Else { return"NO"; } }}
View Code
Three-level linkage
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
View Code$ (document). Ready (function(e) {varzhuti= "<select id= ' sheng ' ></select><select id= ' shi ' ></select><select id= ' qu ' ></ Select> "; $("#sanji"). HTML (Zhuti); //Fill ProvinceFillsheng (); //filled CityFillshi (); //Fill AreaFillqu (); //fill the city and district when the province is selected for change$ ("#sheng"). Change (function(){ //filled CityFillshi (); //Fill AreaFillqu (); }) //to fill the area when the city is selected for change$ ("#shi"). Change (function(){ //Fill AreaFillqu (); }) });//ways to fill the provincefunctionFillsheng () {//Province's parent code varPcode= "0001"; //Tune Ajax$.ajax ({async:false,//Turn off asynchronous, turn on SyncURL: "ajax4chuli.php", Data:{pcode:pcode}, type:"POST", DataType:"TEXT", Success:function(data) {//splits the returned string to get the array of rows varHang=data.trim (). Split ("|"); varStr= ""; for(vari=0;i) { //returns an array of columns varLie=hang[i].split ("^"); STR=str+ "<option value= '" +lie[0]+ ">" +lie[1]+ "</option>"; } $("#sheng"). html (str); } }); }//methods of filling the cityfunctionFillshi () {//take the city's parent code varpcode=$ ("#sheng"). Val (); //Calling Ajax$.ajax ({async:false, URL:"Ajax4chuli.php", Data:{pcode:pcode}, type:"POST", DataType:"TEXT", Success:function(data) {varHang=data.trim (). Split ("|"); varStr= ""; for(vari=0;i) { //returns an array of columns varLie=hang[i].split ("^"); STR=str+ "<option value= '" +lie[0]+ ">" +lie[1]+ "</option>"; } $("#shi"). html (str); } }) }//methods for populating the areafunctionFillqu () {//find the parent code of the zone varpcode=$ ("#shi"). Val (); //Calling Ajax$.ajax ({URL:"Ajax4chuli.php", Data:{pcode:pcode}, type:"POST", DataType:"TEXT", Success:function(data) {varHang=data.trim (). Split ("|"); varStr= ""; for(vari=0;i) { //returns an array of columns varLie=hang[i].split ("^"); STR=str+ "<option value= '" +lie[0]+ ">" +lie[1]+ "</option>"; } $("#qu"). html (str); } }) }
View Code<? phpinclude ("dbda.php"new Dbda (); // Receive parent code $pcode = $_post["Pcode"]; // The child zone is checked according to the parent code $sql = "SELECT * from Chinastates where parentareacode= ' {$pcode} '"; Echo $db-- Strquery ($sql);
View CodeAJAX three-level linkage