当前位置:Gxlcms > 数据库问题 > 继续封装DBDA.php 加入ajax

继续封装DBDA.php 加入ajax

时间:2021-07-01 10:21:17 帮助过:2人阅读

class DBDA { public $host = "localhost"; //服务器地址 public $uid = "root"; //数据库的用户名 public $pwd = "123456"; //数据库的密码 //执行SQL语句,返回相应结果的函数 //$sql是要执行的SQL语句 //$type是SQL语句的类型,0代表增删改,1代表查询 //$db代表要操作的数据库 public function Query($sql,$type=1,$db="xinjian") { //造连接对象 $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; } } public function ajaxQuery($sql,$type=1,$db="xinjian") { //造连接对象 $conn = new MySQLi($this->host,$this->uid,$this->pwd,$db); //判断连接是否成功 !mysqli_connect_error() or die("连接失败!"); //执行SQL语句 $result = $conn->query($sql); $attr = $result->fetch_all(); if($type==1) { $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 { return $result; } } }

 

继续封装DBDA.php 加入ajax

标签:

人气教程排行