DB封装类

<?php
class DBDA
{
public $host = "localhost";
public $uid = "root";
public $pwd = "root";
public $dbname = "mydb"; public function Query($sql,$type=1) //连接数据库,参数默认为1的时候为查询结果,其它的为增删改。
{
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
$result = $db->query($sql); if($type=="1")
{
return $result->fetch_all();
}
else
{
return $result;
}
} public function StrQuery($sql,$type=1) //此方法用于把取出的数据(二维数组)进行字符串拼接处理,用^和|分隔开。
{
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
$result = $db->query($sql); if($type=="1")
{
$arr = $result->fetch_all();
$str = "";
foreach($arr as $v)
{
$str = $str.implode("^",$v)."|";
}
$str = substr($str,0,strlen($str)-1);
return $str;
}
else
{
return $result;
}
} public function JsonQuery($sql,$type=1) //此方法用于ajax中返回的是json数据类型时使用
{
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
$result = $db->query($sql); if($type=="1")
{
$arr = $result->fetch_all(MYSQLI_ASSOC);//括号内为将参数改为关联数组
return json_encode($arr);
}
else
{
return $result;
}
}
}

PDO封装类

<?php
class DBDAP
{
public $host = "localhost";
public $uid = "root";
public $pwd = "root";
public $dbname = "mydb"; public function Query($sql,$type=1)
{
$dsn = "mysql:dbname=$this->dbname;host=$this->host";
$pdo = new PDO($dsn,"$this->uid","$this->pwd");
$result = $pdo->query($sql); if($type=="1") //参数为1时,返回查询结果
{
return $result->fetchall(PDO::FETCH_ASSOC);//括号内的参数必须填写,不然取得的数据会发生重复现象。若不写,返回的数据有关联数据也有索引数据。
}
else
{
$zeng = $pdo->prepare($sql);
if($type=="2") //参数为2时,可以进行预处理语句
{
return $zeng;
}else
{
return $result;
}
}
} public function StrQuery($sql,$type=1) //此方法用于对取出的数据(二维数组)进行拼接字符串处理
{
$dsn = "mysql:dbname=$this->dbname;host=$this->host";
$pdo = new PDO($dsn,"$this->uid","$this->pwd");
$result = $pdo->query($sql); if($type=="1")
{
$arr = $result->fetchall(PDO::FETCH_ASSOC);
$str = "";
foreach($arr as $v)
{
$str = $str.implode("^",$v)."|";
}
$str = substr($str,0,strlen($str)-1);
return $str;
}
else
{
$zeng = $pdo->prepare($sql);
if($type=="2")
{
return $zeng;
}
else
{
return $result;
}
}
}
public function JsonQuery($sql,$type=1) //此方法用于ajax中用于返回为json数据类型时使用
{
$dsn = "mysql:dbname=$this->dbname;host=$this->host";
$pdo = new PDO($dsn,"$this->uid","$this->pwd");
$result = $pdo->query($sql); if($type=="1")
{
$arr = $result->fetchall(PDO::FETCH_ASSOC);
return json_encode($arr);
}
else
{
$zeng = $pdo->prepare($sql);
if($type=="2")
{
return $zeng;
}
else
{
return $result;
}
}
} }

学习到目前,自己封装的db类和pdo类的更多相关文章

  1. 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

  2. Android(java)学习笔记62:继承Thread类创建线程类

    package cn.itcast_02; /* * 该类要重写run()方法,为什么呢? * 不是类中的所有代码都需要被线程执行的. * 而这个时候,为了区分哪些代码能够被线程执行,java提供了T ...

  3. 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类)

    近期工作中有使用到 MongoDb作为日志持久化对象,需要实现对MongoDb的增.删.改.查,但由于MongoDb的版本比较新,是2.4以上版本的,网上已有的一些MongoDb Helper类都是基 ...

  4. 视音频编解码学习工程:FLV封装格式分析器

    ===================================================== 视音频编解码学习工程系列文章列表: 视音频编解码学习工程:H.264分析器 视音频编解码学习 ...

  5. 我的QT5学习之路(三)——模板库、工具类和控件(下)

    一.前言 作为第三篇的最后一部分,我们来看一下Qt的控件,谈到控件,就会让人想到界面的美观性和易操作性,进而想到开发的便捷性.作为windows界面开发的MFC曾经是盛行了多少年,但是其弊端也随着其他 ...

  6. Java学习(API及Object类、String类、StringBuffer字符串缓冲区)

    一.JAVA的API及Object类 1.API 概念: Java 的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就 ...

  7. 设计模式学习(二):面向对象设计原则与UML类图

    一.UML类图和面向对象设计原则简介 在学习设计模式之前,需要找我一些预备知识,主要包括UML类图和面向对象设计原则. UML类图可用于描述每一个设计模式的结构以及对模式实例进行说明,而模式结构又是设 ...

  8. Android(java)学习笔记2:继承Thread类创建线程类

    1. 继承Thread类 创建线程类: package cn.itcast_02; /* * 该类要重写run()方法,为什么呢? * 不是类中的所有代码都需要被线程执行的. * 而这个时候,为了区分 ...

  9. 封装自己DB

    DB.class.php <?php /** * Created by PhpStorm. * User: brady.wang * Date: 2017/11/10 * Time: 18:00 ...

随机推荐

  1. js 跳转到 百度指定地址定位点

    js 跳转到 百度指定地址定位点 http://api.map.baidu.com/geocoder?address=北京市海淀区上地信息路9号奎科科技大厦&output=html&s ...

  2. WebMercator和geographic互相转换

    方法1:esri的sdk中包含的方法:esri.geometry.geographicToWebMercator() 方法2:自己转换 //经纬度转Web墨卡托 function lonLat2Web ...

  3. JAVA 或与非运算符 与(&)、或(|)、异或(^)

    运算步骤: 第一步:.转成二进制,即01表示的数字,如5的二进制为 0000  0101,我用八位表示. 第二步:比较二者位数上的数字 1.与运算符 与运算符用符号“&”表示,其使用规律如下: ...

  4. Windows Vistual Studio 2013/2015 MRPT安装

    博客参考:https://blog.csdn.net/qyjzhou/article/details/80110941 MRPT 安装 1. 官网编译好的程序直接安装 https://sourcefo ...

  5. 报错:flutter: Another exception was thrown: Could not find a generator for route RouteSettings

    原因是一个工程中多次使用MaterialApphttps://stackoverflow.com/questions/49132299/could-not-find-a-generator-for-r ...

  6. ERROR 1130:mysql 1130连接错误的有效解决方法

    今天在用sqlyog连接非本地的Mysql服务器的数据库,居然无法连接很奇怪,报1130错误, ERROR 1130: Host 192.168.3.100 is not allowed to con ...

  7. 使用supervisor支持Python3程序 (解决找不到Module的问题)

    Supervisor是python2写就的一款强大的运维工具(其实现在已经支持Python3了 https://github.com/Supervisor/supervisor)那么怎么利用Super ...

  8. 【Leetcode_easy】867. Transpose Matrix

    problem 867. Transpose Matrix solution: class Solution { public: vector<vector<int>> tra ...

  9. adb中文乱码问题怎么解决?

    1.chcp 65001      执行完命令后 2.在cmd命令弹出的终端页面,右键单击属性设置成字体Lucida Console ,设置完成就解决了 adb 中文乱码问题

  10. bootstrapTable:获取选中行的数据

    必须要有checkbox:true和singleSelect:true,然后就可以通过var row=$("#mytab").bootstrapTable('getSelectio ...