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;
}
}
整理了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;
}
}
相关文章推荐
- asp rs.open sql,conn,3,1中3,1代表什么?
- php+mysqli实现批量执行插入、更新及删除数据的方法
- 让mayfish支持mysqli数据库驱动的实现方法
- PHP5 mysqli的prepare准备语句使用说明
- mysql,mysqli,PDO的各自不同介绍
- php操作mysqli(示例代码)
- PHP以mysqli方式连接类完整代码实例
- mysql与mysqli的区别与用法说明
- php中关于mysqli和mysql区别的一些知识点分析
- PHP使用mysqli扩展连接MySQL数据库
- php中数据库连接方式pdo和mysqli对比分析
- PHP数据库操作之基于Mysqli的数据库操作类库
- php+mysqli实现将数据库中一张表信息打印到表格里的方法
- mysqli多查询特性 实现多条sql语句查询
- PHP mysql与mysqli事务使用说明 分享
- php+mysqli预处理技术实现添加、修改及删除多条数据的方法
- php+mysqli批量查询多张表数据的方法
- php使用mysqli向数据库添加数据的方法
- 解决phpmyadmin中缺少mysqli扩展问题的方法
- php+mysqli实现批量替换数据库表前缀的方法