类:

<?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类的更多相关文章

  1. php四个常用类封装 :MySQL类、 分页类、缩略图类、上传类;;分页例子;

    Mysql类 <?php /** * Mysql类 */ class Mysql{ private static $link = null;//数据库连接 /** * 私有的构造方法 */ pr ...

  2. php 单例模式封装MySQL类

    class MysqlConn { //定义一个私有的静态属性,用来存放实例化的对象 private static $dbcon; //定义一个私有的静态属性,用来存在数据库的连接 private s ...

  3. Python3操作MySQL基于PyMySQL封装的类

    Python3操作MySQL基于PyMySQL封装的类   在未使用操作数据库的框架开发项目的时候,我们需要自己处理数据库连接问题,今天在做一个Python的演示项目,写一个操作MySQL数据库的类, ...

  4. php--->单例模式封装mysql操作类

    php 单例模式封装mysql操作类 单例模式的必要条件(三私一公) 私有的成员属性--防止类外引入这个存放对象的属性 私有的构造方法--为了防止在类外使用new关键字实例化对象 私有的克隆方法--为 ...

  5. MySQL JDBC常用知识,封装工具类,时区问题配置,SQL注入问题

    JDBC JDBC介绍 Sun公司为了简化开发人员的(对数据库的统一)操作,提供了(Java操作数据库的)规范,俗称JDBC,这些规范的由具体由具体的厂商去做 对于开发人员来说,我们只需要掌握JDBC ...

  6. php 封装mysql 数据库操作类

    <?phpheader('content-type:text/html;charset=utf-8');//封装mysql   连接数据库php_mysql//封装mysql   连接数据库ph ...

  7. php 封装Mysql数据库操作类

    花了点时间写了个基于php5.3的Mysql类 $mysql = new Mysql('host','user','pass','db') bool Mysql::insert("表&quo ...

  8. PDO 增删改查封装的类

    Selecting Data 你在mysql_*中是这样做的 <?php $result = mysql_query('SELECT * from table') or die(mysql_er ...

  9. PHPEXCEL在thinkphp中封装成类使用

    PHPEXCEL在thinkphp中封装成类使用 标签: phpexcel导出导入thinkphp -- : 435人阅读 评论() 收藏 举报 分类: php() 版权声明:本文为博主原创文章,未经 ...

随机推荐

  1. hibernate manytoone中的lazy EAGER

    Hibernate中的字段映射中的Fetch有两种方式:EAGER和LAZY Eager:全部抓取 Lazy:延迟抓取 如果在字段中声明为Eager,那么在取得当前Bean时,同时会抓取Bean中的关 ...

  2. extjs MVC模式的个人看法

    针对一个后台管理页面是mvc模式,后台也是mvc模式下的项目,要怎么去熟悉呢? 首先以我个人的认解,先从后台的管理界面来看,会有control,model,store,view:其中先看view的代码 ...

  3. ::after,::before使用

    ::after,::before使用   1.:before 选择器在被选元素的内容前面插入内容. 请使用 content 属性来指定要插入的内容. <!DOCTYPE html> < ...

  4. JavaScript判断字符串的字符长度(中文占两个字符)

    判断方法 //判断字符串中的字符 中文算两个字符 function chkstrlen(str) { ; ; i < str.length; i++) { ) //如果是汉字,则字符串长度加2 ...

  5. PIVOT&UNPIVOT

    如果是家电销售员,那么可能需要统计每月日销售的彩电.冰箱.空调...最大值.最小值.平均值等 如果你是耳鼻喉科医生,那么可能需要统计月度年度日接客咽炎.喉炎.鼻炎...最大值.最小值.平均值等 如果你 ...

  6. SVN hooks强制提交时填写日志

    #!/bin/bash REPOS="$1" TXN="$2" #svnlook路径 SVNLOOK=/usr/bin/svnlook #通过svnlook获取 ...

  7. [LeetCode] Divide Two Integers( bit + 二分法 )

    Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...

  8. DiG HOWTO How to use dig to query DNS name servers.

    Contents Introduction Understanding the default output What can I discover? How do I … Get a short a ...

  9. Web service是什么?

    原文:http://www.ruanyifeng.com/blog/2009/08/what_is_web_service.html 我认为,下一代互联网软件将建立在Web service(也就是&q ...

  10. Mysqldump 参数详解(全)

    Mysqldump 参数详解(全) http://www.open-open.com/lib/view/open1358172843762.html mysqldump -S /tmp/mysql33 ...