继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

MySQL连接类,重温学习之路

朝夕_
关注TA
已关注
手记 1
粉丝 5
获赞 33
<?php 

/**
 * 数据库操作类`输入代码`
 * date : 2016-04-29
 */

// $config 应放在配置文件中
$config = array(
        'host'          =>   'localhost',     //服务器地址
        'user'          =>   'root',          //用户名
        'pwd'           =>   'root',          //密码
        'db_name'       =>   'test',          //数据库名
        'db_char'       =>   'utf8'           //字符集
        );

class Mysql{

    private $conn;
    private $config = array();
    static private $_instance;

    //禁止直接实例化对象
    private function __construct($config){
        $this->config = $config;
        try {
            $this->conn = mysql_connect($this->config['host'], $this->config['user'], $this->config['pwd']);
            if(!$this->conn){
                throw new Exception("数据库连接失败");
            }
            mysql_select_db($config['db_name'], $this->conn);
            mysql_set_charset($config['db_char'], $this->conn);
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    //禁止克隆
    private function __clone(){}

    //单例模式获取对象实例
    static public function getInstance($config){
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self($config);
        }
        return self::$_instance;
    }

    //数据库执行语句,可执行查询添加修改删除等任何sql语句
    public function query($sql){
        if ($sql == '') {
            throw new Exception('SQL 语句错误');
        } else {
            $result = mysql_query($sql, $this->conn);
            if (!$result) {
                throw new Exception(mysql_errno().': '.mysql_error());
            }
            return $result;
        }
    }

    //获取一个数据 如count()
    public function getOne($sql){
        try {
            $result = $this->query($sql);
            $row = mysql_fetch_row($result);
            return $row[0];

        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    //获取一行数据 一维数组
    public function getRow($sql){
        try {
            $result = $this->query($sql);
            return mysql_fetch_assoc($result);
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    //获取所有数据 多维数组
    public function getAll($sql){
        try {
            $result = $this->query($sql);
            while ($row = mysql_fetch_assoc($result)) {
                $all[] = $row; 
            }
            return $all;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * [add 添加数据]
     * @param [string] $table [数据表]
     * @param [array] $data   [数据]
     */
    public function add($table, $data){
        try {
            if (!is_array($data)) throw new Exception('参数类型错误');

            $value_str = implode('","' , $data);
            $sql = 'INSERT INTO '.$table;

            //判断数组类型(索引 OR 关联)
            if (($field = array_keys($data)) == array_keys(array_keys($data))) {
                $sql .= ' VALUES ("'.$value_str.'")';
            } else {
                $field_str = implode(',' , $field);
                $sql .= ' ('.$field_str.') VALUES ("'.$value_str.'")';
            }

            $result =  $this->query($sql);
            return $result;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * [edit 修改数据]
     * @param  [string]   $table [数据表]
     * @param  [array]    $data  [数据]
     * @param  [string]   $where [条件]
     * @return [bool]          
     */
    public function edit($table, $data, $where = null){
        try {
            if (!is_array($data)) throw new Exception('参数类型错误');
            $sql = 'UPDATE '.$table.' SET ';
            foreach ($data as $k => $v) {
                $sql .= $k.'="'.$v.'",';
            }
            $sql = rtrim($sql,',');

            if ($where) {
                $sql .= ' WHERE '.$where;
            }

            $result =  $this->query($sql);
            return $result;
        } catch (Exception $e) {
            return $e->getMessage();
        }

    }

    //获得最新添加数据的Id
    public function getInsertId(){
        return mysql_insert_id();
    }

    //获得受影响的行数
    public function getAffectedRows(){
        return mysql_affected_rows();
    }

}

有问题请大家指教

打开App,阅读手记
9人推荐
发表评论
随时随地看视频慕课网APP

热门评论

这个类看起来很实用型,如果能把使用示例也整理下就更好了

查看全部评论