MySql数据库操作类


   
   getField($sql); 获取某一字段值
 * $db->getRow($sql);   获取某一行数据
 * $db->getList($sql);  获取多行数据
 * $db->exe($sql);      执行数据库操作:insert update delete....
 */
class MySqlDB
{
    private $host = 'localhost';
    private $user = 'root';
    private $passwd = 'loving';
    private $connection = null;
    private $db_name = null;
    private $resultSet = null;
    private $sql = '';
    
    private static $instance = null;
    
    private function __construct($host, $port, $user, $passwd) {
        $this->host = $host.($port ? ':'.$port : '');
        $this->user = $user;
        $this->passwd = $passwd;
        $this->connection = mysql_connect($this->host, $this->user, $this->passwd) or die ('can not connect!'.mysql_error());
    }
    
    public static function getDB($host, $port, $user, $passwd, $db_name) {
        $key = md5($host.'-'.$port.'-'.$user.'-'.$passwd);
        if (MySqlDB::$instance == null || MySqlDB::$instance[$key] == null){
            MySqlDB::$instance[$key] = new MySqlDB($host, $port, $user, $passwd);
        }
        MySqlDB::$instance[$key]->db_name = $db_name;
        mysql_query("SET NAMES utf8", MySqlDB::$instance[$key]->connection);
        mysql_select_db($db_name, MySqlDB::$instance[$key]->connection);
        return MySqlDB::$instance[$key];  
    }

    protected function _query($sql) {
        $this->sql = $sql;
        $this->resultSet = mysql_query($this->sql, $this->connection);
        return mysql_num_rows($this->resultSet);
    }

    /**
     * 执行SQL语句
     * @param string $sql SQL指令
     * @return false | integer
     */
    public function exe($sql) {
        $this->sql = $sql;
        $this->resultSet = mysql_query($this->sql, $this->connection);
        return mysql_affected_rows();
    }
    
    /**
     * 查询某一字段
     * @param string $sql 查询语句
     * @param string $is_array 返回结果集是否是数组
     * @return mixed
     */
    public function getField($sql, $is_array = false) {
        $result = $this->_query($sql);
        if ($result === false){
            return false;
        }
        if( false == $is_array ){
            $res = mysql_fetch_array($this->resultSet, MYSQL_NUM);
            return $res[0];
        }else{
	        while ( $row = mysql_fetch_array($this->resultSet, MYSQL_ASSOC) ){
	            $res[] = $row[0];
	        }
	        return $res;
        }
    }
    
    /**
     * 获取当行数据
     * @param string $sql 查询语句
     * @return mixed
     */
    public function getRow($sql) {
        $result = $this->_query($sql);
        if ($result === false){
            return false;
        }
        return mysql_fetch_array($this->resultSet, MYSQL_ASSOC);
    }
    
    /**
     * 获取多行数据
     * @param string $sql   查询语句
     * @param string $key   字段名称,false: 返回索引数组; field_name: 返回键值数组
     * @param string $value 字段名称,false: 返回键值多维数组;field_name: 返回键值二位数组
     * @return mixed
     */
    public function getList($sql, $key = false, $value = false) {
        $result = $this->_query($sql);
        if ($result === false){
            return false;
        }
        if( false == $key ){
	        while ( $row = mysql_fetch_array($this->resultSet, MYSQL_ASSOC) ){
	            $res[] = $row;
	        }
        }elseif( false == $value ){
            while ( $row = mysql_fetch_array($this->resultSet, MYSQL_ASSOC) ){
                $res[$row[$key]] = $row;
            }
        }else{
            while ( $row = mysql_fetch_array($this->resultSet, MYSQL_ASSOC) ){
                $res[$row[$key]] = $row[$value];
            }
        }
        return $res;
    }
    
    public function __destruct() {
        mysql_close($this->connection);
        MySqlDB::$instance = null;
    }
}
?>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值