《PHP實例:PHP封裝返回Ajax字符串和JSON數組的方法》要點:
本文介紹了PHP實例:PHP封裝返回Ajax字符串和JSON數組的方法,希望對您有用。如果有疑問,可以聯系我們。
實例如下:PHP應用
<?php class DBDA { public $host="localhost"; public $uid = "root"; public $pwd = "123"; public $dbname = "mydb"; //成員方法 public function Query($sql,$type=1) { $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname); $r = $db->query($sql); if($type==1) { return $r->fetch_all(); } else { return $r; } } //返回字符串的方法 public function StrQuery($sql,$type=1) { $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname); $r = $db->query($sql); if($type==1) { $attr = $r->fetch_all(); $str = ""; foreach($attr as $v) { $str .= implode("^",$v)."|"; } return substr($str,0,strlen($str)-1); } else { return $r; } } //返回JSON function JSONQuery($sql,$type=1) { $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname); $r = $db->query($sql); if($type==1) { return json_encode($r->fetch_all(MYSQLI_ASSOC)); } else { return $r; } } }
以上這篇PHP封裝返回Ajax字符串和JSON數組的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持維易PHP.PHP應用
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/1768.html