mysqli工具类

1 篇文章 0 订阅
<?php

/*
 * mysqli工具类 
 */
class Mysqlis{

	private $mysqli = '';
	private $result = '';
	private $address = 'localhost';
	private $user = 'root';
	private $password = 'root';
	private $database = 'test';
	private $port = '3306';

	public function __construct($add='', $user='', $pwd='', $db='', $port=''){
		$this->address  = empty($add)?$this->address:$add;
		$this->user     = empty($user)?$this->user:$user;
		$this->password = empty($pwd)?$this->password:$pwd;
		$this->database = empty($db)?$this->database:$db;
		$this->port     = empty($port)?$this->port:$port;
		$this->mysqli = new mysqli($this->address, $this->user, $this->password, $this->database,$this->port);
		// 判断是否连接失败 
		// connect_errno: return a error code
		// connect_error: return a error description
		if($this->mysqli->connect_errno){
			die('connect mysql fail,the error is: '.$this->mysqli->connect_error);
		}
		

	}

	/*
    * query:对数据库执行一次查询
	*/
	public function query($sql){
		$flag = $this->mysqli->query($sql);
		if(!$flag){
			return false;
		}
	}

	/*
    * 获取一个二维数组
    * return array(array())
	*/
	public function db_select($arr, $table, $where=''){
		if($arr == '*'){
			$fields = '*';
		}else{
			if(!is_array($arr)){
				return false;
				exit;
			}
			$fields = implode(',', array_values($arr));
		}
		$sql = " SELECT $fields FROM $table $where";
		$this->result = $this->query($sql);
		$tmp = array();
		while($row = $this->result->fetch_array(MYSQLI_ASSOC)){
			$tmp[] = $row;
		}
		$this->result->free();
		return $tmp;
	}

	/*
    * 获取一条数据
    * reurn array()
	*/
	public function db_getone($arr, $table, $where=''){
		if($arr == '*'){
			$fields = '*';
		}else{
			if(!is_array($arr)){
				return false;
				exit;
			}
			$fields = implode(',', array_values($arr));
		}
		$sql = " SELECT $fields FROM $table $where LIMIT 1";
		$this->result = $this->query($sql);
		$row = $this->result->fetch_array(MYSQLI_ASSOC);
		$this->result->free();
		return $row;
	}

	/*
    * 获取一个字段的值
    * reurn string
	*/
	public function db_getfield($str, $table, $where=''){
		if($str == '*'){
			return false;
		}
		$sql = " SELECT $str FROM $table $where";
		$this->result = $this->query($sql);
		return $this->result;
		$row = $this->result->fetch_array(MYSQLI_ASSOC);
		$this->result->free();
		return $row[$str];
	}

	/*
 	* 插入
	*/
	public function db_insert($arr, $table){
		// insert aa=1 into teset
		$fields = implode(',', array_keys($arr));
		$values = array_values($arr);
		foreach($values as $k=>$v){
			$values[$k] = '"'.$v.'"';
		}
		$values = implode(',', $values);
		$sql = "INSERT INTO $table($fields) VALUES($values)";
		$this->query($sql);
		$id = $this->mysqli->insert_id;
		return $id;
	}

	/**
	 * 更新字段
	 */
	public function db_update($arr , $table, $where=''){
		if(!is_array($arr) || count($arr)==0){
			return false;
		}
		// update table set field=value
		$tmp = '';
		foreach($arr as $k=>$v){
			$tmp.='`'.$k."`='".$v."',";
		}
		$tmp = rtrim($tmp, ',');
		$sql = "UPDATE {$table} SET {$tmp} {$where}";
		$result = $this->query($sql);
		$result = is_null($result)?true:false;
		return $result;
	}


 	/*
 	* 删除
	*/
 	public function db_delete($table, $where=''){
 		$sql = "DELETE FROM {$table} {$where}";
 		$result = $this->query($sql);
 		$result = is_null($result)?true:false;
		return $result;
 	}

	/*
     * 切换数据库
	 */
	public function select_db($db){
		// select_db:切换数据库,成功返回true,失败返回false
		$flag = $this->mysqli->select_db($db);
		if(!$flag){
			die('switch database fail !the error is: '.$this->mysqli->error);
		}
	}

	/*
    * 设置默认字符编码
	*/
	public function set_charset($str){
		// set_charset:设置字符编码,成功返回true,失败返回false
		$flag = $this->mysqli->set_charset($str);
		if(!$flag){
			die('set charset fail !the error is: '.$this->mysqli->error);
		}
	}

	public function __destruct(){
		// 关闭mysql连接
		$this->mysqli->close();
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值