首先引入类
<?php class DBDA { public $host = "localhost"; //服务器地址 public $uid = "root"; //数据库的用户名 public $pwd = "123"; //数据库的密码 //执行SQL语句,返回相应结果的函数 //$sql是要执行的SQL语句 //$type是SQL语句的类型,0代表增删改,1代表查询 //$db代表要操作的数据库 public function Query($sql,$type=1,$db="mydb") { //造连接对象 $conn = new MySQLi($this->host,$this->uid,$this->pwd,$db); //判断连接是否成功 !mysqli_connect_error() or die("连接失败!"); //执行SQL语句 $result = $conn->query($sql); //判断SQL语句类型 if($type==1) { //如果是查询语句返回结果集的二维数组 return $result->fetch_all(); } else { //如果是其他语句,返回true或false return $result; } } //Ajax调用返回JSON public function JsonQuery($sql,$type=1,$db="mydb") { //定义数据源 $dsn = "mysql:dbname={$db};host={$this->host}"; //造pdo对象 $pdo = new PDO($dsn,"{$this->uid}","{$this->pwd}"); //准备执行SQL语句 $st = $pdo->prepare($sql); //执行预处理语句 if($st->execute()) { if($type==1) { $attr = $st->fetchAll(PDO::FETCH_ASSOC); return json_encode($attr); } else { if($st) { return "OK"; } else { return "NO"; } } } else { echo "执行失败!"; } } //Ajax调用返回字符串 public function StrQuery($sql,$type=1,$db="mydb") { //造连接对象 $conn = new MySQLi($this->host,$this->uid,$this->pwd,$db); //判断连接是否成功 !mysqli_connect_error() or die("连接失败!"); //执行SQL语句 $result = $conn->query($sql); //判断SQL语句类型 if($type==1) { $attr = $result->fetch_all(); $str = ""; //如果是查询语句返回字符串 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 { //如果是其他语句,返回true或false if($result) { return "OK"; } else { return "NO"; } } } }
返回类型为json
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="../jquery-1.11.2.min.js"></script> </head> <body> </body> <script type="text/javascript"> $(document).ready(function(e) { $.ajax({ url:"chuli.php", data:{}, dataType:"JSON", type:"POST", success: function(data){ /* for(key in data) { alert(data[key].Name); }*/ for(var i=0;i<data.length;i++) { alert(data[i].Name); } } }); }); </script> </html>
处理页面
1 <?php 2 3 include("DBDA.php"); 4 $db = new DBDA(); 5 6 $sql = "select * from Nation"; 7 8 echo $db->JsonQuery($sql);
返回类型为text就调用DBDA里的返回字符串的方法
返回类型为xml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="../jquery-1.11.2.min.js"></script> <title>无标题文档</title> </head> <body> </body> <script type="text/javascript"> $(document).ready(function(e) { $.ajax({ url:"chulixml.php", data:{}, dataType:"XML", type:"POST", success: function(data){ alert($(data).find("Name").eq(0).text());//eq(1)时就查处理页面的n2 } }); }); </script> </html>
处理页面
<?php $str = <<<AA <Info> <n1> <Code>n001</Code> <Name>张三</Name> <Sex>男</Sex> </n1> <n2> <Code>n002</Code> <Name>李四</Name> <Sex>女</Sex> </n2> </Info> AA; echo $str; ?>