热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP以mysqli方式连接类完整代码实例

这篇文章主要介绍了PHP以mysqli方式连接类完整代码实例,对于学习和了解mysqli都有很大的帮助,需要的朋友可以参考下
本文所述的是一个在PHP中以mysqli方式连接数据库的一个数据库类实例,该数据库类是从一个PHP的CMS中整理出来的,可实现PHP连接数据库类,MySQLi版,兼容PHP4,对于有针对性需要的朋友可根据此代码进行优化和修改。

<&#63;php
#==================================================================================================
# Filename: /db/db_mysqli.php
# Note : 连接数据库类,MySQLi版
#==================================================================================================
#[类库sql]
class db_mysqli
{
 var $query_count = 0;
 var $host;
 var $user;
 var $pass;
 var $data;
 var $conn;
 var $result;
 var $prefix = "qinggan_";
 //返回结果集类型,默认是数字+字符
 var $rs_type = MYSQLI_ASSOC;
 var $query_times = 0;#[查询时间]
 var $conn_times = 0;#[连接数据库时间]
 var $unbuffered = false;
 //定义查询列表
 var $querylist;
 var $debug = false;
 #[构造函数]
 function __construct($cOnfig=array())
 {
 $this->host = $config['host'] &#63; $config['host'] : 'localhost';
 $this->port = $config['port'] &#63; $config['port'] : '3306';
 $this->user = $config['user'] &#63; $config['user'] : 'root';
 $this->pass = $config['pass'] &#63; $config['pass'] : '';
 $this->data = $config['data'] &#63; $config['data'] : '';
 $this->debug = $config["debug"] &#63; $config["debug"] : false;
 $this->prefix = $config['prefix'] &#63; $config['prefix'] : 'qinggan_';
 if($this->data)
 {
  $ifcOnnect= $this->connect($this->data);
  if(!$ifconnect)
  {
  $this->cOnn= false;
  return false;
  }
 }
 return true;
 }
 #[兼容PHP4]
 function db_mysqli($cOnfig=array())
 {
 return $this->__construct($config);
 }
 #[连接数据库]
 function connect($database="")
 {
 $start_time = $this->time_used();
 if(!$this->port) $this->port = "3306";
 $this->cOnn= @mysqli_connect($this->host,$this->user,$this->pass,"",$this->port) or false;
 if(!$this->conn)
 {
  return false;
 }
 $version = $this->get_version();
 if($version>"4.1")
 {
  mysqli_query($this->conn,"SET NAMES 'utf8'");
  if($version>"5.0.1")
  {
  mysqli_query($this->conn,"SET sql_mode=''");
  }
 }
 $end_time = $this->time_used();
 $this->conn_times += round($end_time - $start_time,5);#[连接数据库的时间]
 $ifok = $this->select_db($database);
 return $ifok &#63; true : false;
 }
 function select_db($data="")
 {
 $database = $data &#63; $data : $this->data;
 if(!$database)
 {
  return false;
 }
 $this->data = $database;
 $start_time = $this->time_used();
 $ifok = mysqli_select_db($this->conn,$database);
 if(!$ifok)
 {
  return false;
 }
 $end_time = $this->time_used();
 $this->conn_times += round($end_time - $start_time,5);#[连接数据库的时间]
 return true;
 }
 #[关闭数据库连接,当您使用持续连接时该功能失效]
 function close()
 {
 if(is_resource($this->conn))
 {
  return mysqli_close($this->conn);
 }
 else
 {
  return true;
 }
 }
 function __destruct()
 {
 return $this->close();
 }
 function set($name,$value)
 {
 if($name == "rs_type")
 {
  $value = strtolower($value) == "num" &#63; MYSQLI_NUM : MYSQLI_ASSOC;
 }
 $this->$name = $value;
 }
 function query($sql)
 {
 if(!is_resource($this->conn))
 {
  $this->connect();
 }
 else
 {
  if(!mysql_ping($this->conn))
  {
   $this->close();
   $this->connect();
  }
 }
 if($this->debug)
 {
  $sqlkey = md5($sql);
  if($this->querylist)
  {
  $qlist = array_keys($this->querylist);
  if(in_array($sqlkey,$qlist))
  {
   $count = $this->querylist[$sqlkey]["count"] + 1;
   $this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>$count);
  }else{
   $this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>1);
  }
  }
  else{
  $this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>1);
  }
 }
 $start_time = $this->time_used();
 $func = $this->unbuffered && function_exists("mysqli_multi_query") &#63; "mysqli_multi_query" : "mysqli_query";
 $this->result = @$func($this->conn,$sql);
 $this->query_count++;
 $end_time = $this->time_used();
 $this->query_times += round($end_time - $start_time,5);#[查询时间]
 if(!$this->result)
 {
  return false;
 }
 return $this->result;
 }
 function get_all($sql="",$primary="")
 {
 $result = $sql &#63; $this->query($sql) : $this->result;
 if(!$result)
 {
  return false;
 }
 $start_time = $this->time_used();
 $rs = array();
 $is_rs = false;
 while($rows = mysqli_fetch_array($result,$this->rs_type))
 {
  if($primary && $rows[$primary])
  {
  $rs[$rows[$primary]] = $rows;
  }
  else
  {
  $rs[] = $rows;
  }
  $is_rs = true;
 }
 $end_time = $this->time_used();
 $this->query_times += round($end_time - $start_time,5);#[查询时间]
 return ($is_rs &#63; $rs : false);
 }
 function get_one($sql="")
 {
 $start_time = $this->time_used();
 $result = $sql &#63; $this->query($sql) : $this->result;
 if(!$result)
 {
  return false;
 }
 $rows = mysqli_fetch_array($result,$this->rs_type);
 $end_time = $this->time_used();
 $this->query_times += round($end_time - $start_time,5);#[查询时间]
 return $rows;
 }
 function insert_id($sql="")
 {
 if($sql)
 {
  $rs = $this->get_one($sql);
  return $rs;
 }
 else
 {
  return mysqli_insert_id($this->conn);
 }
 }
 function insert($sql)
 {
 $this->result = $this->query($sql);
 $id = $this->insert_id();
 return $id;
 }
 function all_array($table,$cOndition="",$orderby="")
 {
 if(!$table)
 {
  return false;
 }
 $table = $this->prefix.$table;
 $sql = "SELECT * FROM ".$table;
 if($condition && is_array($condition) && count($condition)>0)
 {
  $sql_fields = array();
  foreach($condition AS $key=>$value)
  {
  $sql_fields[] = "`".$key."`='".$value."' ";
  }
  $sql .= " WHERE ".implode(" AND ",$sql_fields);
 }
 if($orderby)
 {
  $sql .= " ORDER BY ".$orderby;
 }
 $rslist = $this->get_all($sql);
 return $rslist;
 }
 function one_array($table,$cOndition="")
 {
 if(!$table)
 {
  return false;
 }
 $table = $this->prefix.$table;
 $sql = "SELECT * FROM ".$table;
 if($condition && is_array($condition) && count($condition)>0)
 {
  $sql_fields = array();
  foreach($condition AS $key=>$value)
  {
  $sql_fields[] = "`".$key."`='".$value."' ";
  }
  $sql .= " WHERE ".implode(" AND ",$sql_fields);
 }
 $rslist = $this->get_one($sql);
 return $rslist;
 }
 //将数组写入数据中
 function insert_array($data,$table,$insert_type="insert")
 {
 if(!$table || !is_array($data) || !$data)
 {
  return false;
 }
 $table = $this->prefix.$table;//自动增加表前缀
 if($insert_type == "insert")
 {
  $sql = "INSERT INTO ".$table;
 }
 else
 {
  $sql = "REPLACE INTO ".$table;
 }
 $sql_fields = array();
 $sql_val = array();
 foreach($data AS $key=>$value)
 {
  $sql_fields[] = "`".$key."`";
  $sql_val[] = "'".$value."'";
 }
 $sql.= "(".(implode(",",$sql_fields)).") VALUES(".(implode(",",$sql_val)).")";
 return $this->insert($sql);
 }
 //更新数据
 function update_array($data,$table,$condition)
 {
 if(!$data || !$table || !$condition || !is_array($data) || !is_array($condition))
 {
  return false;
 }
 $table = $this->prefix.$table;//自动增加表前缀
 $sql = "UPDATE ".$table." SET ";
 $sql_fields = array();
 foreach($data AS $key=>$value)
 {
  $sql_fields[] = "`".$key."`='".$value."'";
 }
 $sql.= implode(",",$sql_fields);
 $sql_fields = array();
 foreach($condition AS $key=>$value)
 {
  $sql_fields[] = "`".$key."`='".$value."' ";
 }
 $sql .= " WHERE ".implode(" AND ",$sql_fields);
 return $this->query($sql);
 }
 function count($sql="")
 {
 if($sql)
 {
  $this->rs_type = MYSQLI_NUM;
  $this->query($sql);
  $rs = $this->get_one();
  $this->rs_type = MYSQLI_ASSOC;
  return $rs[0];
 }
 else
 {
  return mysqli_num_rows($this->result);
 }
 }
 function num_fields($sql="")
 {
 if($sql)
 {
  $this->query($sql);
 }
 return mysqli_num_fields($this->result);
 }
 function list_fields($table)
 {
 $rs = $this->get_all("SHOW COLUMNS FROM ".$table);
 if(!$rs)
 {
  return false;
 }
 foreach($rs AS $key=>$value)
 {
  $rslist[] = $value["Field"];
 }
 return $rslist;
 }
 #[显示表名]
 function list_tables()
 {
 $rs = $this->get_all("SHOW TABLES");
 return $rs;
 }
 function table_name($table_list,$i)
 {
 return $table_list[$i];
 }
 function escape_string($char)
 {
 if(!$char)
 {
  return false;
 }
 return mysqli_escape_string($this->conn,$char);
 }
 function get_version()
 {
 return mysqli_get_server_info($this->conn);
 }
 function time_used()
 {
 $time = explode(" ",microtime());
 $used_time = $time[0] + $time[1];
 return $used_time;
 }
 //Mysql的查询时间
 function conn_times()
 {
 return $this->conn_times + $this->query_times;
 }
 //MySQL查询资料
 function conn_count()
 {
 return $this->query_count;
 }
 # 高效SQL生成查询,仅适合单表查询
 function phpok_one($tbl,$cOndition="",$fields="*")
 {
 $sql = "SELECT ".$fields." FROM ".$this->db->prefix.$tbl;
 if($condition)
 {
  $sql .= " WHERE ".$condition;
 }
 return $this->get_one($sql);
 }
 function debug()
 {
 if(!$this->querylist || !is_array($this->querylist) || count($this->querylist) <1)
 {
  return false;
 }
 $html = '
'; $html.= ''; $html.= ''; foreach($this->querylist AS $key=>$value) { $html .= ''; $html .= ''; } $html.= "
SQL查询

'.$value['sql'].'

'.$value["count"].'

"; $html.= "
"; return $html; } function conn_status() { if(!$this->conn) return false; return true; } } &#63;>

推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 数据库(外键及其约束理解)(https:www.cnblogs.comchenxiaoheip6909318.html)My ... [详细]
author-avatar
cang桑哥哥
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有