封装mysql类
类:
<?php
header("content-type:text/html;charset=utf-8");
//封装一个类
/*
掌握满足单例模式的必要条件
(1)私有的构造方法-为了防止在类外使用new关键字实例化对象
(2)私有的成员属性-为了防止在类外引入这个存放对象的属性
(3)私有的克隆方法-为了防止在类外通过clone成生另一个对象
(4)公有的静态方法-为了让用户进行实例化对象的操作
*/
class db{
//三私一共
//私有的静态属性
private static $dbcon=false;
//私有的构造方法
private function __construct(){
$dbcon=@mysql_connect("localhost","root","root");
mysql_select_db("small2",$dbcon) or die("mysql_connect error");
mysql_query("set names utf8");
}
//私有的克隆方法
private function __clone(){}
//公用的静态方法
public static function getIntance(){
if(self::$dbcon==false){
self::$dbcon=new self;
}
return self::$dbcon;
}
//打印数据
public function p($arr){
echo "<pre>";
print_r($arr);
echo "</pre>";
}
public function v($arr){
echo "<pre>";
var_dump($arr);
echo "</pre>";
}
//执行语句
public function query($sql){
$query=mysql_query($sql);
return $query;
}
/**
* 查询某个字段
* @param
* @return string or int
*/
public function getOne($sql){
$query=$this->query($sql);
return mysql_result($query,0);
}
//获取一行记录,return array 一维数组
public function getRow($sql,$type="assoc"){
$query=$this->query($sql);
if(!in_array($type,array("assoc",'array',"row"))){
die("mysql_query error");
}
$funcname="mysql_fetch_".$type;
return $funcname($query);
}
//获取一条记录,前置条件通过资源获取一条记录
public function getFormSource($query,$type="assoc"){
if(!in_array($type,array("assoc","array","row")))
{
die("mysql_query error");
}
$funcname="mysql_fetch_".$type;
return $funcname($query);
}
//获取多条数据,二维数组
public function getAll($sql){
$query=$this->query($sql);
$list=array();
while ($r=$this->getFormSource($query)) {
$list[]=$r;
}
return $list;
}
//获得最后一条记录id
public function getInsertid(){
return mysql_insert_id();
}
/**
* 定义添加数据的方法
* @param string $table 表名
* @param string orarray $data [数据]
* @return int 最新添加的id
*/
public function insert($table,$data){
//遍历数组,得到每一个字段和字段的值
$key_str='';
$v_str='';
foreach($data as $key=>$v){
if(empty($v)){
die("error");
}
//$key的值是每一个字段s一个字段所对应的值
$key_str.=$key.',';
$v_str.="'$v',";
}
$key_str=trim($key_str,',');
$v_str=trim($v_str,',');
//判断数据是否为空
$sql="insert into $table ($key_str) values ($v_str)";
$this->query($sql);
//返回上一次增加操做产生ID值
return mysql_insert_id();
}
/*
* 删除一条数据方法
* @param1 $table, $where=array('id'=>'1') 表名 条件
* @return 受影响的行数
*/
public function deleteOne($table, $where){
if(is_array($where)){
foreach ($where as $key => $val) {
$condition = $key.'='.$val;
}
} else {
$condition = $where;
}
$sql = "delete from $table where $condition";
$this->query($sql);
//返回受影响的行数
return mysql_affected_rows();
}
/*
* 删除多条数据方法
* @param1 $table, $where 表名 条件
* @return 受影响的行数
*/
public function deleteAll($table, $where){
if(is_array($where)){
foreach ($where as $key => $val) {
if(is_array($val)){
$condition = $key.' in ('.implode(',', $val) .')';
} else {
$condition = $key. '=' .$val;
}
}
} else {
$condition = $where;
}
$sql = "delete from $table where $condition";
$this->query($sql);
//返回受影响的行数
return mysql_affected_rows();
}
/**
* [修改操作description]
* @param [type] $table [表名]
* @param [type] $data [数据]
* @param [type] $where [条件]
* @return [type]
*/
public function update($table,$data,$where){
//遍历数组,得到每一个字段和字段的值
$str='';
foreach($data as $key=>$v){
$str.="$key='$v',";
}
$str=rtrim($str,',');
//修改SQL语句
$sql="update $table set $str where $where";
$this->query($sql);
//返回受影响的行数
return mysql_affected_rows();
}
}
?>
测试:
//mysql测试
//$db=db::getIntance();
//var_dump($db);
/*$sql="select * from acticle";
$list=$db->getAll($sql);
$db->p($list);*/
/*$sql="select * from acticle where acticle_id=95";
$list=$db->getRow($sql);
$db->p($list);
*/
/*$sql="select title from acticle";
$list=$db->getOne($sql);
$db->p($list);*/
//$list=$db->insert("users",$_POST);
//$del=$db->deleteOne("users","id=26");
//$del=$db->deleteAll("users","id in(23,24)");
//$up=$db->update("users",$_POST,"id=27");
//$id=$db->getInsertid();
//print_R($id);
封装mysql类的更多相关文章
- php四个常用类封装 :MySQL类、 分页类、缩略图类、上传类;;分页例子;
Mysql类 <?php /** * Mysql类 */ class Mysql{ private static $link = null;//数据库连接 /** * 私有的构造方法 */ pr ...
- php 单例模式封装MySQL类
class MysqlConn { //定义一个私有的静态属性,用来存放实例化的对象 private static $dbcon; //定义一个私有的静态属性,用来存在数据库的连接 private s ...
- Python3操作MySQL基于PyMySQL封装的类
Python3操作MySQL基于PyMySQL封装的类 在未使用操作数据库的框架开发项目的时候,我们需要自己处理数据库连接问题,今天在做一个Python的演示项目,写一个操作MySQL数据库的类, ...
- php--->单例模式封装mysql操作类
php 单例模式封装mysql操作类 单例模式的必要条件(三私一公) 私有的成员属性--防止类外引入这个存放对象的属性 私有的构造方法--为了防止在类外使用new关键字实例化对象 私有的克隆方法--为 ...
- MySQL JDBC常用知识,封装工具类,时区问题配置,SQL注入问题
JDBC JDBC介绍 Sun公司为了简化开发人员的(对数据库的统一)操作,提供了(Java操作数据库的)规范,俗称JDBC,这些规范的由具体由具体的厂商去做 对于开发人员来说,我们只需要掌握JDBC ...
- php 封装mysql 数据库操作类
<?phpheader('content-type:text/html;charset=utf-8');//封装mysql 连接数据库php_mysql//封装mysql 连接数据库ph ...
- php 封装Mysql数据库操作类
花了点时间写了个基于php5.3的Mysql类 $mysql = new Mysql('host','user','pass','db') bool Mysql::insert("表&quo ...
- PDO 增删改查封装的类
Selecting Data 你在mysql_*中是这样做的 <?php $result = mysql_query('SELECT * from table') or die(mysql_er ...
- PHPEXCEL在thinkphp中封装成类使用
PHPEXCEL在thinkphp中封装成类使用 标签: phpexcel导出导入thinkphp -- : 435人阅读 评论() 收藏 举报 分类: php() 版权声明:本文为博主原创文章,未经 ...
随机推荐
- 对象(List<T>)的序列化和反序列化
本文描述将对象(List<T>)序列化到 XML 文档中和从 XML 文档中反序列化为对象(List<T>). 命名空间: System.Xml.Serialization 程 ...
- QcheckBox
#include "dialog.h" #include "ui_dialog.h" #include <QtCore> #include < ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
- js获取网站根目录
//js获取网站根路径(站点及虚拟目录),获得网站的根目录或虚拟目录的根地址 function getRootPath(){ var strFullPath=window ...
- 【转】Android 获得view的宽和高
转自:http://blog.csdn.net/yangdeli888/article/details/25405263 Android 获得view的宽和高 分类: android 技术点项目20 ...
- table tricks
- php 如何进入mysql数据库
我是初学者,有两台电脑,进入数据库通用的方法直接找mysql.exe,如下例: F:\Program Files\wamp\bin\mysql\mysql5.5.20\bin
- String的类型的数据
字符串类型的数据也是计算机基础. var box = "lc"; var box1 = 'lc1'; //不管是单引号还双引号,都必须成对出现 1.ECMAscript规定字符串是 ...
- ionic 报错%1 is not a valid Win32 application
Fixed the problem by installing python version 3.0 and above will do下载Python3.0或以上版本 python官网传送门:htt ...
- jenkins自动构建截图