mysql mysqli封装_PHP封装MySQLi数据库操作类

/**

* Created by PhpStorm.

* User: Sudo

* Date: 2017/7/11

* Time: 20:22

*/

//设置编码格式(防止乱码)

header("Content-type: text/html; charset=utf-8");

// 1 同壹个类,方法和变量必须加$this->变量/方法,但参数不需要

// 2 变量定义必须加上权限修饰符(封装)

// 3 常量定义 const 不需要加权限修饰符和$

// 4 类中的构造函数只能有一个

class Tools {

private $link = null;

function __construct(){

$this->connectDB();

}

function connectDB(){

$this ->link = mysqli_connect("localhost","root","root")

or die($this -> link = null);

if(!mysqli_select_db($this->link,"queen")){

$this ->link = null;

}

}

//查询数据库

// @param $tableNmae 要求传string类型的值 表名

// @param $col 默认为* 要求传array类型的值 列名

// @param $where 默认为"" 要求传array类型的值 条件

function selectDB($tableName,$col = "*",$where = ""){

//若数据库连接标识符为空则尝试继续连接数据库

if($this ->link == null){

$this -> connectDB();

//return "error";

}

//根据传递的数组元素拼接查询的字段

$cols = "";

if($col == "*"){

$cols = "*";

}else{

for($i = 0,$ilen = count($col);$i

//最后一个数组元素不拼接逗号

if($i < $ilen -1){

$cols = $cols.$col[$i].",";

continue;

}

$cols = $cols.$col[$i];

}

}

//根据传递的数组元素拼接查询的条件

$whereTemp == "";

if($where == ""){

$whereTemp == "";

}else{

for($i = 0,$ilen = count($where);$i

if($i < $ilen -1){

$whereTemp = $whereTemp.$where[$i].",";

continue;

}

$whereTemp = $whereTemp.$where[$i];

}

}

//构建sql语句

$sql = "";

if($whereTemp == ""){

//如果查询条件为空则不加条件

$sql = "select ".$cols." from ".$tableName;

}else{

//有查询条件时加上查询条件

$sql = "select ".$cols." from ".$tableName." where ".$whereTemp;

}

//执行查询语句返回结果集

$result = mysqli_query($this->link,$sql);

//关闭数据库

mysqli_close($this->link);

//如果刚才查询语句执行错误则返回error,否则返回查询的结果

if(mysqli_affected_rows($this->link) < 0){

return "error";

}else{

$arr = array();

//将查询结果存入数组$row中,再将$row添加到$arr数组中并返回

while($row = mysqli_fetch_row($result)){

array_push($arr, $row);

}

return $arr;

}

}

//插入数据到数据库表中

//参数一:表名$tableName,字符串

//参数二:字段名$arrCols,数组

//参数三:字段值$arrValues,数组

function insertDB($tableName,$arrCols,$arrValues){

if($this ->link == null){

$this -> connectDB();

}

//根据数组元素拼接要插入的字段名

$tempCols = "(";

for($i = 0,$ilen = count($arrCols);$i

if($i < $ilen -1){

$tempCols = $tempCols.$arrCols[$i].",";

continue;

}

$tempCols = $tempCols.$arrCols[$i];

}

$tempCols = $tempCols.")";

//根据数组元素拼接要插入的字段值

$tempValues = "(";

for($i = 0,$ilen = count($arrValues);$i

if($i < $ilen -1){

//如果插入表中的字段值是字符串则直接插入相应字段,否则转换成字符串再插入,最后一个字段值不拼接逗号

if(is_string($arrValues[$i])){

$tempValues = $tempValues."'".$arrValues[$i]."'".",";

}else{

$tempValues = $tempValues.$arrValues[$i].",";

}

continue;

}

if(is_string($arrValues[$i])){

$tempValues = $tempValues."'".$arrValues[$i]."'";

}else{

$tempValues = $tempValues.$arrValues[$i];

}

}

$tempValues = $tempValues.")";

//构建sql插入语句并执行该语句

$sql = "insert into ".$tableName. $tempCols." values".$tempValues;

mysqli_query($this->link,$sql);

//如果刚才的插入语句影响的纪录行数大于零,说明插入成功,关闭数据库并返回true,否则返回false

if(mysqli_affected_rows($this->link) > 0){

mysqli_close($this->link);

return true;

}else{

mysqli_close($this->link);

return false;

}

}

//更新数据库纪录

//参数一:表名$tableName,字符串

//参数二:字段名$colrows,数组

//参数三:字段值$values,数组

//参数四:作为更新依据的某一个特定字段名和字段值$where,字符串

function updateDB($tableName,$colrows,$values,$where){

if($this->link == null){

$this ->connectDB();

}

$str = "";

for($i = 0,$ilen = count($colrows);$i

if($i < $ilen -1){

//根据传递的数组元素拼接将要更新的“字段名=‘新值’”,用逗号分隔;

if(is_string($values[$i])){

//如果新的字段值$values[$i]是字符串,则加上单引号,否则不加

$str = $str.$colrows[$i]."="."'".$values[$i]."'".",";

}else{

$str = $str.$colrows[$i]."=".$values[$i].",";

}

}else{

//最后壹个数组元素(即最后一个字段名=‘新值’)结束后不再拼接逗号

if(is_string($values[$i])){

$str = $str.$colrows[$i]."="."'".$values[$i]."'";

}else{

$str = $str.$colrows[$i]."=".$values[$i];

}

}

}

//构建更新纪录的sql语句,若执行成功并且该更新操作确实影响到数据库表中纪录则返回true,否则返回false

$sql = "update ".$tableName." set ".$str." where ".$where;

mysqli_query($this->link,$sql);

if(mysqli_affected_rows($this->link) >= 0){

return true;

}else{

return false;

}

}

//删除数据库纪录

function deleteDB($tableName,$where){

if($this->link == null){

$this ->connectDB();

}

//根据给定字段的键值组合删除表中相关纪录内容,成功返回true,失败返回false

$sql = "delete from ".$tableName." where ".$where;

mysqli_query($this->link,$sql);

if(mysqli_affected_rows($this->link) >= 0){

return true;

}else{

return false;

}

}

//封装一个上传文件后将对应的服务器临时副本拷贝到指定目录的类,文件名为“原始文件名+服务器当前日期时间”格式

function moveFile($name,$username){

move_uploaded_file($_FILES[$name]["tmp_name"], "upload/".$username.$_FILES[$name]["name"].date("Y-m-d-h-i-s"));

}

//获取客户端通过Ajax提交Form表单的数据类,返回数组

function getClientData($arr){

$arrTemp = array();

for($i = 0,$ilen = count($arr);$i

array_push($arrTemp, $_REQUEST[$arr[$i]]);

}

return $arrTemp;

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值