<?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();
}
}
02-01