您的位置:首页 > 编程语言 > PHP开发

php中mysqli扩展封装数据库类conn(mysqli实例)

2015-11-11 14:52 627 查看
最近在拿一些小模块练练手,上网找了一个用mysql封装的数据库类,deprecated报错让我好是厌烦,然后搜索了mysqli的用法,结合了之前代码,

整理了mysqli扩展封装数据库类conn,接下来练手过程中会看需要添加一些新的函数,若有冗余或错误,请大家多多指教。

<?php

class Mysqli_conn{
private $host = 'localhost';
//服务器地址
private $name = 'root';
//登录账号
private $pwd = '';
//登录密码
private $dBase = '';
//数据库名称
private $conn = '';
//数据库链接资源
private $result = '';
//结果集
private $msg = '';
//返回操作结果
private $fields;
//返回字段
private $fieldsNum = 0;
//返回字段数
private $rowsNum = 0;
//返回结果数
private $rowsRst = '';
//返回单条记录的字段数组
private $fieldssArray = array();
//返回字段数组
private $rowsArray = array();
//返回结果数组
//初始化类
function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '')
$this->host = $host;
if($name != '')
$this->name = $name;
if($pwd != '')
$this->pwd = $pwd;
if($dBase != '')
$this->dBase = $dBase;
$this->init_conn();
}
//链接数据库
function init_conn(){
$this->conn=mysqli_connect($this->host,$this->name,$this->pwd);
mysqli_select_db($this->conn,$this->dBase);
}
//查询结果
function mysqli_query_rst($sql){
if($sql=='')die("SQL can not be null!STOP RUNNING");
if($this->conn == ''){
$this->init_conn();
}
$this->result = mysqli_query($this->conn,$sql);
}
//取得字段数 
function get_Fields_Num($sql){
$this->mysqli_query_rst($sql);
if(mysqli_errno($this->conn)==0){
$this->fieldsNum = mysqli_num_fields($this->result);
return $this->fieldsNum;
}else
return mysqli_error($this->conn);
}
//取得查询结果数
function get_Rows_Num($sql){
$this->mysqli_query_rst($sql);
if(mysqli_errno($this->conn) == 0){
return mysqli_num_rows($this->result);
}else{
return '';
}
}
//取得记录数组
function get_Rows_Array($sql){
$this->mysqli_query_rst($sql);
if(mysqli_errno($this->conn) == 0){
while($row = mysqli_fetch_array($this->result)) {
$this->rowsArray[] = $row;
}
return $this->rowsArray;
}else{
return '';
}
}
//更新、删除、添加记录数
function uidRst($sql){
if($this->conn == ''){
$this->init_conn();
}
mysqli_query($this->conn,$sql);
$this->rowsNum = mysqli_affected_rows($this->conn);
if(mysqli_errno($this->conn) == 0){
return $this->rowsNum;
}else{
return '';
}
}
//获取对应的字段值
function getFields($sql,$fields){
$this->mysqli_query_rst($sql);
if(mysqli_errno($this->conn) == 0){
if(mysqli_num_rows($this->result) > 0){
$tmpfld = mysqli_fetch_row($this->result);
$this->fields = $tmpfld[$fields];

}
return $this->fields;
}else{
return '';
}
}

//错误信息,若数据库操作成功则返回true,失败则返回错误信息
function msg_error(){
if(mysqli_errno($this->conn) != 0) {
$this->msg = mysqli_error($this->conn);
return $this->msg;
}
return true;
}
//释放结果集
function close_rst(){
if(mysqli_errno($this->conn) == 0)mysqli_free_result($this->result);
$this->msg = '';
$this->fieldsNum = 0;
$this->rowsNum = 0;
$this->filesArray = '';
$this->rowsArray = '';
}
//关闭数据库
function close_conn(){
$this->close_rst();
mysqli_close($this->conn);
$this->conn = '';
}
/*
*扩展函数
*arrayToInsertSql($array,$table_name)

*$aray:为$row=>$fields=>$value二维数组,$row>1,将该二维数组转化成sql语句,执行该sql语句即可将该数组插入到数据库中

*/
function arrayToInsertSql($array,$table_name){
$sql = '';
if(is_array($array)){
$keys = array_keys($array[0]);
$sql = "INSERT INTO `".$table_name."` (";
foreach($keys as $v){
$sql.= "`".$v."`,";
//Mysql中字段名用的是”·“,反单引号,不是”‘“,这是一个小坑。
}
$sql = substr($sql,0,strlen($sql)-1);

//截取掉末尾的逗号,末尾若出现逗号,会提示syntax error,但就是不具体告诉你错在哪里,所以有时候若你自己写的sql语句出现这种错误,echo一下看看是不是末尾多了逗号什么的

$sql .=") VALUES";
$rows = array();
foreach($array as $k => $row){
$rows[$k] = '';//('value1','value2','value3','value4'),
foreach($row as $v){
$rows[$k].="'".$v."',";
}
$rows[$k] = substr($rows[$k],0,strlen($rows[$k])-1);//截取掉末尾的逗号
$sql .= "(".$rows[$k]."),";
$rows[$k] = '';
}
$sql = substr($sql,0,strlen($sql)-1);
}
return $sql;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息