<?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";
}
}
}

}

DBDA的更多相关文章

  1. dbda封装类(包括:返回二维数组、Ajax调用返回字符串、Ajax调用返回JSON)

    <?php class DBDA { public $host = "localhost"; public $uid = "root"; public $ ...

  2. dbda数据库类

    <?phpclass DBDA{ public $host="localhost";//服务器地址 public $uid="root";//用户名 pu ...

  3. 继续封装DBDA.php 加入ajax

    <?php class DBDA { public $host = "localhost"; //服务器地址 public $uid = "root"; ...

  4. 链接数据库代码封装DBDA

    <?php class DBDA { public $host = "localhost"; //服务器地址 public $uid = "root"; ...

  5. 2016/04/18 ①注册 注册处理 ② 审核 审核处理 ③登录 登录处理 ④需要jquery-1.11.2.min.js DBDA.php

    ① 注册   zhuceye.php <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  6. 2016/4/5 Ajax ①用户名 密码 登陆 注册 ② 判断用户名是否已存在 ③点击按钮出现民族选项下拉菜单 ④DBDA类 加入Ajaxquery方法 数组变字符串 字符串拆分

    ①登陆   注册    查表匹配    0405Ajax.php   ②判断用户名是否存在 <!DOCTYPE html> <html lang="en"> ...

  7. 2016/3/26 连接数据库 网页中数据的增删改 add delete update addchuli updateChuLi test8 DBDA

    主页面 test8.php <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  8. 我的DBDA类

    <?php class DBDA { public $host="localhost"; public $uid="root"; public $pwd= ...

  9. 0606-工厂模式、单例模式、DBDA的单例和完整功能

    工厂模式:只要指定类名,就可以据此获取一个该类的对象. 单例模式:某个类,只允许其“创建”出一个对象. 单例的方法:三私一公(一个私有化对象,一个私有化构造方法,一个私有化克隆方法,一个公共方法返回对 ...

随机推荐

  1. BZOJ1179 : [Apio2009]Atm 缩点+spfa

    1179: [Apio2009]Atm Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 2069  Solved: 826[Submit][Status ...

  2. 500 OOPS: vsftpd: refusing to run with writable root inside chroot () 不能上传文件和文件夹

    500 OOPS: vsftpd: refusing to run with writable root inside chroot () 问题的是因为用户的根目录可写,并且使用了chroot限制,而 ...

  3. arm-linux交叉编译工具链的制作(基于S3C2440)【转】

    本文转载自:http://eric-gao.iteye.com/blog/2160622 制作arm-linux交叉编译工具链一般通过crosstool工具或者crosstool-NG,前者使用方便, ...

  4. ubuntu 16.04 Eclipse 图标显示为 ?(已解决)

    这个问题挺好解决: sudo gedit /usr/share/applications/eclipse.desktop在这个文件中将Icon=/home/soyo/eclipse/icon.xpm, ...

  5. Ruby检验变量

      更新: 2017/06/12 更新: 2017/06/16 补充.class的输出 更新: 2017/06/23 .include?检验数组/哈希表是否包含目标值 更新: 2017/07/02 b ...

  6. bzoj 1783: [Usaco2010 Jan]Taking Turns【贪心+dp】

    不知道该叫贪心还是dp 倒着来,记f[0][i],f[1][i]分别为先手和后手从n走到i的最大值.先手显然是取最大的,当后手取到比先手大的时候就交换 #include<iostream> ...

  7. bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】

    这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便 先按起始时间从小到大排序. 我用的是greater重定义优先队列(小根堆).用pair存牛棚用完时间(first)和牛棚编号(second),每 ...

  8. bzoj 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路【Floyd】

    弗洛伊德之后按序列加起来即可 #include<iostream> #include<cstdio> #include<algorithm> using names ...

  9. SP1557 GSS2 - Can you answer these queries II(线段树)

    传送门 线段树好题 因为题目中相同的只算一次,我们可以联想到HH的项链,于是考虑离线的做法 先把所有的询问按$r$排序,然后每一次不断将$a[r]$加入线段树 线段树上维护四个值,$sum,hix,s ...

  10. ADSI和其他内容

    ADSI (Active Directory Services Interface)是Microsoft推出的一项技术,它统一了许多底层服务的编程接口,程序员可以使用一致的对象技术来访问这些底层服务. ...