<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Processing interface
<?php include ("dbda.php"), $db =new Dbda (); $pcode =$_post["Pcode"]; $sql = "SELECT * from Chinastates where Parentareacode = ' {$pcode} ', Echo $db->strquery ($sql);
Encapsulate Class Code
<?phpclass dbda{public $host = "localhost";//server address public $uid = "root";//user name public $pwd = "";//password public $conn;//Connection object// How to manipulate the database//$SQL represents the SQL statement that needs to be executed//$type represents the type of SQL statement, 1 for the query, 0 for the delete//$DB represents the database name to manipulate//if it is a query, returns a two-dimensional array//if it is another statement, Returns TRUE or Falsefunction __construct ($db = "AAAS") {//Build Connection object $this->conn = new Mysqli ($this->host, $this->uid,$ This->pwd, $db);} Public Function Query ($sql, $type =1) {//Determine if error!mysqli_connect_error () or Die ("Connection failed! ");//Execute SQL statement $result = $this->conn->query ($sql);//Determine SQL statement type if ($type ==1) {///If it is a query statement, returns a two-dimensional array of result sets return $result- >fetch_all ();} else{//if it is a different statement, returns TRUE or Falsereturn $result;}} The AJAX call returns Jsonpublic function Jsonquery ($sql, $type =1, $db = "AAAS") {//define data source $dsn = "mysql:dbname={$db};host={$this- >host} ";//Create PDO object $pdo = new PDO ($dsn," {$this->uid} "," {$this->pwd} ");//prepare to execute SQL statement $st = $pdo->prepare ($sql) ;//execute preprocessing SQL statement if ($st->execute ()) {if ($type ==1) {$sttr = $st->fetchall (PDO::FETCH_ASSOC); return Json_encode ($ attr);} Else{if ($st) {return "OK";} Else{return "NO";}}} Else{echo "Execution failed!";}} The AJAX call returns the string public function strquery ($sql, $type =1) {//determines if the connection was successful!mysqli_connect_error () or Die ("Connection failed! ");//Execute SQL statement $result = $this->conn->query ($sql);//Determine SQL statement type if ($type ==1) {$attr = $result->fetch_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{//if it is a different statement, return TRUE or Falseif ($result) {return "OK";} Else{return "NO";}}} function Pdoquery ($sql, $type =1, $db = "AAAS") {//build data source $dns = "mysql:host={$this->host};d bname={$db}";//PDO Object $PDO = New PDO ($dns, $this->uid, $this->pwd);//prepare an SQL statement $stm = $pdo->prepare ($sql);//execute preprocessing statement = $stm->execute () if ($r) {if ($type ==1) {return $stm->fetchall ();} Else{return "OK";}} Else{return "NO";}}}
Three-level linkage display information of Chinese provinces and cities