<?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. 容器ArrayList原理(学习)

    一.概述 动态数组,容量能动态增长,元素可以为null,用数组存储,非线程同步(vector线程同步) 每个 ArrayList 实例都有一个容量,该容量是指用来存储列表元素的数组的大小,自动增长(默 ...

  2. 洛谷 P1181,1182 数列分段Section

    数列分段Section I 题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成连续的若干段,并且每段和不超过M(可以等于M),问最少能将其分成多少段使得满足要求. 输入输出格式 输入格 ...

  3. Cpp module

  4. 21. Ext中表格自适应高度

    转自:https://blog.csdn.net/happy492/article/details/6401099 1. 下面的代码中width和height的初始值为tab的开始大小,当浏览器窗口变 ...

  5. FastDFS的介绍

    FastDFS的介绍 FastDFS FastDFS是由国人余庆所开发,其项目地址: https://github.com/happyfish100 FastDFS是一个轻量级的开源分布式文件系统,主 ...

  6. bzoj 1691: [Usaco2007 Dec]挑剔的美食家【贪心+splay】

    高端贪心,好久没写splay调了好久-- 以下v为价格,w为鲜嫩度 把牛和草都按v排升序,扫草,首先把v小于等于当前草的牛都丢进splay,这样一来splay里全是可选的牛了,按w排序,然后贪心的为当 ...

  7. 10.27night清北刷题班

    /* 枚举每个部分的总和,利用前缀和进行检验. 如果能分成4部分就一定能分成2部分,就筛了一边素数优化.清空数组!!! */ #include<bits/stdc++.h> #define ...

  8. SpringBoot2.x版本整合SpringSecurity、Oauth2进行password认证

    很多人在进行项目开发时都会用到Oauth2.0结合SpringSecurity或者Shiro进行权限拦截以及用户验证,网上也有很多的案例,前几天项目里边需要用到,顺便整合了进来,特此写篇博客,记录下过 ...

  9. c++ swap函数

    swap(a,b)也就是把a和b的值互换. 头文件:#include<algorithm>,swap要加using namespace std:

  10. MySQL故障处理一例_Another MySQL daemon already running with the same unix socket

    MySQL故障处理一例:"Another MySQL daemon already running with the same unix socket". [root@test- ...