<?php
header('content-type:text/html;charset=utf-8');
//封装mysql   连接数据库php_mysql
//封装mysql   连接数据库php_mysqli
//封装mysql   连接数据库php_pdo
class db{
    //三私一共
    //私有的静态属性
    private static $dbcon = false; //存储数据库连接对象
    //私有的构造方法
    private function __construct(){
        //创建链接
        $dbcon=mysql_connect('127.0.0.1','root','root');
        //选择数据库
        mysql_select_db('yii_back',$dbcon) or die('mysql connect error');
        //设置字符集
        mysql_query('set names utf8');
    }
    //私有的clone
    private function __clone(){}
    //共有的静态方法
    public static function getInstance(){
        if(self::$dbcon == false){
            self::$dbcon = new self;
        }
        return self::$dbcon;
    }

/**
    *执行sql语句
    *@param $sql
    *@return source
    **/
    public function query($sql){
        //通过mysql_query来实现
        $query = mysql_query($sql);
        
        //返回执行结果
        return $query;
    }

/**
     * 查询某个字段  eg:  select  name  select count(*)
     *@param $sql
     *@return string or int
    **/
    public function getOne($sql){
        $query=$this->query($sql);
        //有可能出现错误
        if(!$query){
            //SQL语句有错
            echo 'SQL语句错误!<br/>';
            echo '错误编号:' . mysql_errno() . '<br/>';
            echo '错误原因:' . mysql_error() . '<br/>';
            exit;
        }    
        return mysql_result($query,0);
    }

/**
     * 查询一行记录
     *@param $sql
     *@return array 一维数组
     *mysql_fetch_assoc  mysql_fetch_array  mysql_fetch_row
    **/
    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);
    }

/**
     * 查询多条记录
     *@param $sql
     *@return array 二维数组
     *mysql_fetch_assoc  mysql_fetch_array  mysql_fetch_row
    **/
    public function getAll($sql){
        //调用sql 执行函数
        $query=$this->query($sql);
        $list=array();
        //遍历结果
        //while($arr = $this->getRowFromSource($query)){
        while($arr = mysql_fetch_assoc($query)){
            $list[]=$arr;
        }
        return $list;
    }

/**
     * 获得上一次插入的id
    **/
    public function getInsertId(){
        return mysql_insert_id();
    }

/**
     * 新增数据方法
     * @param1 string $sql,要执行的SQL语句
     * @param1 array  $data,要添加的数据
     * @return 自增长id
    **/
    public function insert($table,$data=array()){  
        //print_r($data);die;
        $sql="insert into ".$table."(".implode(',',array_keys($data)).") values ('".implode("','",array_values($data))."')";
        //调用sql 执行函数
        $query=$this->query($sql);
        if($query){
            //返回数据
            return mysql_insert_id();  //得到上次操作的自增长ID
        }else{
            return $sql;
        }
    }

/**
     * 修改数据方法
     * @param string $table 操作的数据表名
     * @param array $data 操作的数据
     * @param array $condition 条件
    **/
    public function update($table,$data,$condition=array()){
        $where='';
        if(!empty($condition)){   
            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        $updatastr = '';
        if(!empty($data)){
            foreach($data as $k=>$v){
                $updatastr.= $k."='".$v."',";
            }
            $updatastr = 'set '.rtrim($updatastr,',');
        }
        $sql = "update {$table} {$updatastr} {$where}";
        $query=$this->query($sql);
        if($query){
            echo '修改成功';
        }else{
            return $sql;
        }
    }

/**
     * 删除数据方法
     * @param string $table 操作的数据表名
     * @param array  $condition 删除的条件
     */
    public function delete($table,$condition){
        $where='';
        if(!empty($condition)){     
            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        $sql="delete from {$table} {$where}";
        $query=$this->query($sql);
        if($query){
            echo '删除成功';
        }else{
            return $sql;
        }
    }

/**
     * 查询数据方法
     * @param string $table 操作的数据表名
     * @param array  $condition 查询的条件
     * @param array  $field     要查询的字段
     */
    public function select($table,$condition=array(),$field = array()){
        $where='';
        if(!empty($condition)){
             
            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        $fieldstr = '';
        if(!empty($field)){
            foreach($field as $k=>$v){
                $fieldstr.= $v.',';
            }
            $fieldstr = rtrim($fieldstr,',');
        }else{
            $fieldstr = '*';
        }
        $sql = "select {$fieldstr} from {$table} {$where}";
        $query=$this->query($sql);
        $resultRow = array();
        $i = 0;
        while($row=mysql_fetch_assoc($query)){
            foreach($row as $k=>$v){
                $resultRow[$i][$k] = $v;
            }
            $i++;
        }
        return $resultRow;
    }
}

php 封装mysql 数据库操作类的更多相关文章

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

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

  2. php : mysql数据库操作类演示

    设计目标: 1,该类一实例化,就可以自动连接上mysql数据库: 2,该类可以单独去设定要使用的连接编码(set names XXX) 3,该类可以单独去设定要使用的数据库(use XXX): 4,可 ...

  3. php MySQL数据库操作类源代码

    php MySQL数据库操作类源代码: <?php class MySQL{ private $host; //服务器地址 private $name; //登录账号 private $pwd; ...

  4. 设计模式 - 单例模式mysql数据库操作类

    待续... index.php 调用方法: <?php header('Content-Type:text/html; charset=utf8'); require 'instance.php ...

  5. MySQL数据库操作类(PHP实现,支持连贯操作)

    <?php /** * Author: suvan * CreateTime: 2018/2/27 * description: 数据库操作类(仅对接MySQL数据库,主要利用MySQLi函数) ...

  6. php pdo mysql数据库操作类

    <?php namespace iphp\core; use iphp\App; /** * 数据库操作基类 基于pdo * @author xuen * 支持链式操作,支持参数绑定 * 说明1 ...

  7. C# MySQL 数据库操作类

    using System; using System.Configuration; using System.Collections; using System.Data; using MySql.D ...

  8. DELPHI XE MYSQL数据库操作类 MYSQLHELPER

    注: 无需odbc配置 {* * MySQL Helper v1.0 * 2015.6.19 * 说明: * 这是一个操作MySQL的类,该类必须和libmysql.dll,dbxmys.dll两个文 ...

  9. php中用面向对象的思想编写mysql数据库操作类

    最近刚入门完mysql,正好学了一阵子php就想着如何把mysql的表信息用php打印页面上.现在就把代码贴出来,以便小伙伴们参考. 先是建立mysql连接: /*建立连接*/ class datab ...

随机推荐

  1. rsyslog日志服务的配置文件分析

    基于rsyslog日志服务的日志 在不同的LINUX系统,实现的软件略有不同. syslog,rsyslog,syslog-ng,用于实现系统日志的管理. [root@asianux4 ~]# rpm ...

  2. PostgreSQL Hardware Performance Tuning

    Bruce Momjian POSTGRESQL is an object-relational database developed on the Internet by a group of de ...

  3. linux oracle profile配置

    [oracle@db01 ~]$ more .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashrc ...

  4. syslog-ng 安装

    下载 Syslog-NG的rpm包,  地址 http://www.kevindeng.org/wp-content/uploads/2010/10/Syslog-NG.zip unzip解压 [ro ...

  5. JSP 相关试题(一)

    选择题 1.当用户请求jsp页面时,JSP引擎就会执行该页面的字节码文件响应客户的请求,执行字节码文件的结果是(C) A)发送一个JSP源文件到客户端    B)发送一个Java文件到客户端 C)发送 ...

  6. 虚拟化之vmware-vsphere (web) client

    两种客户端 vsphere client 配置>软件>高级设置里的变量 uservars.supressshellwarning=1 vsphere web client 安装完vSphe ...

  7. unity,生成的mac版游戏切场景时卡死解法

    unity版本为5.1.1,在编辑器里运行没问题,build出的windows版运行也没问题,但build出的mac版在个别场景切换时会卡死,通过查看log(查看build版本log的方法参考:htt ...

  8. C++ string::size_type 类型【转】

    int main() { string str("Hello World!\n"); cout << "The size of " << ...

  9. wamp下Apache2.4.x局域网访问403的解决办法

    1.我们打开Apache目录\wamp\bin\apache\apache2.4.9下的“conf”文件夹,找到httpd.conf. 2.找到#   onlineoffline tag - don' ...

  10. 【性能诊断】八、并发场景的性能分析(windbg案例,连接泄露)

    此前遇到一个项目反馈系统宕机问题,摘要描述如下: 系统不定期出现卡死现象,在多个模块不同功能上都出现过,未发现与特定功能相关的明显规律: 当系统出现卡死现象时,新的用户无法登陆系统: 跟踪应用服务器, ...