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. ifc osg施工现场模拟

    基于ifc数据模型的施工现场模拟

  2. python flask框架学习(二)——第一个flask程序

    第一个flask程序 学习自:知了课堂Python Flask框架——全栈开发 1.用pycharm新建一个flask项目 2.运行程序 from flask import Flask # 创建一个F ...

  3. 静态站点生成器-md-vue-vuepress

    推荐指数:

  4. Github克隆代码慢问题解决办法

    参考:https://blog.csdn.net/stone8761/article/details/79072148 https://blog.csdn.net/github_37847975/ar ...

  5. 10点睛Spring4.1-Application Event

    10.1 Application Event Spring使用Application Event给bean之间的消息通讯提供了手段 应按照如下部分实现bean之间的消息通讯 继承Application ...

  6. setInterval【计时器】

    ~function(){ const nextPushTime = $('#nextPushTime_promotion_push'); const prevTime = new Date('2017 ...

  7. PHP与Cookie

    不管什么语言写的cookie,本质上没区别. cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 PH ...

  8. LeetCode 442. 数组中重复的数据(Find All Duplicates in an Array) 17

    442. 数组中重复的数据 442. Find All Duplicates in an Array 题目描述 Given an array of integers, 1 ≤ a[i] ≤ n (n ...

  9. 综述论文翻译:A Review on Deep Learning Techniques Applied to Semantic Segmentation

    近期主要在学习语义分割相关方法,计划将arXiv上的这篇综述好好翻译下,目前已完成了一部分,但仅仅是尊重原文的直译,后续将继续完成剩余的部分,并对文中提及的多个方法给出自己的理解. 论文地址:http ...

  10. JDK1.8 的 HashMap 源码之注意事项

    文章目录 链表变树 树形结构与Comparable,性能极致与降低 链表与树之间转换的阈值 英语渣靠着翻译插件,大概翻译的,难免有错误之处,注意甄别: 链表变树 This map usually ac ...