DBDA
<?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的更多相关文章
- dbda封装类(包括:返回二维数组、Ajax调用返回字符串、Ajax调用返回JSON)
<?php class DBDA { public $host = "localhost"; public $uid = "root"; public $ ...
- dbda数据库类
<?phpclass DBDA{ public $host="localhost";//服务器地址 public $uid="root";//用户名 pu ...
- 继续封装DBDA.php 加入ajax
<?php class DBDA { public $host = "localhost"; //服务器地址 public $uid = "root"; ...
- 链接数据库代码封装DBDA
<?php class DBDA { public $host = "localhost"; //服务器地址 public $uid = "root"; ...
- 2016/04/18 ①注册 注册处理 ② 审核 审核处理 ③登录 登录处理 ④需要jquery-1.11.2.min.js DBDA.php
① 注册 zhuceye.php <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- 2016/4/5 Ajax ①用户名 密码 登陆 注册 ② 判断用户名是否已存在 ③点击按钮出现民族选项下拉菜单 ④DBDA类 加入Ajaxquery方法 数组变字符串 字符串拆分
①登陆 注册 查表匹配 0405Ajax.php ②判断用户名是否存在 <!DOCTYPE html> <html lang="en"> ...
- 2016/3/26 连接数据库 网页中数据的增删改 add delete update addchuli updateChuLi test8 DBDA
主页面 test8.php <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- 我的DBDA类
<?php class DBDA { public $host="localhost"; public $uid="root"; public $pwd= ...
- 0606-工厂模式、单例模式、DBDA的单例和完整功能
工厂模式:只要指定类名,就可以据此获取一个该类的对象. 单例模式:某个类,只允许其“创建”出一个对象. 单例的方法:三私一公(一个私有化对象,一个私有化构造方法,一个私有化克隆方法,一个公共方法返回对 ...
随机推荐
- SQL Server 运行计划操作符具体解释(1)——断言(Assert)
前言: 非常多非常多地方对于语句的优化,一般比較靠谱的回复即使--把运行计划发出来看看.当然那些仅仅看语句就说怎样怎样改代码,我一直都是拒绝的,由于这样的算是纯蒙.依据本人经验,大量的性能问题单纯从语 ...
- 深度理解apache 重写模块rewrite_mod,重写不再犯错
1.RewriteRule ^(com\/.*)$ index.php?do=$1 问:上面的规则匹配表达式 "^(.*)$" 匹配的内容是什么 答:匹配内容是URI站点目录:/d ...
- win下IE设置
当win7系统时需要升级IE为11版本,需要先安装sp1版本补丁,再装IE11,若还是装不了,可借助第三方平台(电脑管家等)升级安装.或 更新系统再安装IE11 https://jingyan.bai ...
- linux 点命令
cat a date . a Mon Jun :: CST linux .(点命令):读取并且在当前的shell中执行文件中的命令
- SoapUI中的RegEx
在for Content matching RegEx中
- Applications using Launch Screen Files and targetting iOS 7.1 and earlier need to also include a Launch Image in an Asset Catalog.
在使用xcode6建的项目时,有时在ios7模拟器下会出现一下情况 导航栏上方和tabbar下方会有黑边 并且会有一下警告: Applications using Launch Screen File ...
- [JSOI 2008] 星球大战
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1015 [算法] 考虑离线 , 将删点转化为加点 , 用并查集维护连通性即可 时间复杂 ...
- MySQL5.7修改字符集
本人安装的mysql版本是5.7.20,安装好mysql后就要对字符集进行修改了,于是照着网上的大部分教程说的去安装目录找一个my-default.ini文件,然后重命名为my.ini,再对其进修改字 ...
- bzoj4818
http://www.lydsy.com/JudgeOnline/problem.php?id=4818 矩阵快速幂+dp 首先我们来写一个dp dp[i][j]:选到第i个数,和为j,复杂度nm,不 ...
- Spark GraphX 聚合操作
package Spark_GraphX import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.graph ...