搜索
首页php教程php手册php 数据库mysql查询与连接类

php 数据库mysql查询与连接类

Jun 13, 2016 am 10:09 AM
classhostmysqlphpuservar数据库查询连接


class mysql
{
 var $host     = "";   //mysql主机名
 var $user     = "";   //mysql用户名
 var $pwd      = "";   //mysql密码
 var $dbName   = "";   //mysql数据库名称
 var $linkID   = 0;   //用来保存连接ID
 var $queryID  = 0;   //用来保存查询ID
 var $fetchMode= MYSQL_ASSOC;//取记录时的模式
 var $queryTimes = 0;  //保存查询的次数
 var $errno    = 0;   //mysql出错代号
 var $error    = "";   //mysql出错信息
 var $record   = array(); //一条记录数组

 //======================================
 // 函数: mysql()
 // 功能: 构造函数
 // 参数: 参数类的变量定义
 // 说明: 构造函数将自动连接数据库
 //       如果想手动连接去掉连接函数
 //======================================
 function mysql($host,$user,$pwd,$dbName)
 { if(empty($host) || empty($user) || empty($dbName))
   $this->halt("数据库主机地址,用户名或数据库名称不完全,请检查!");
  $this->host    = $host;
  $this->user    = $user;
  $this->pwd     = $pwd;
  $this->dbName  = $dbName;
  $this->connect();//设置为自动连接
 }
 //======================================
 // 函数: connect($host,$user,$pwd,$dbName)
 // 功能: 连接数据库
 // 参数: $host 主机名, $user 用户名
 // 参数: $pwd 密码, $dbName 数据库名称
 // 返回: 0:失败
 // 说明: 默认使用类中变量的初始值
 //======================================
 function connect($host = "", $user = "", $pwd = "", $dbName = "")
 {
  if ("" == $host)
   $host = $this->host;
  if ("" == $user)
   $user = $this->user;
  if ("" == $pwd)
   $pwd = $this->pwd;
  if ("" == $dbName)
   $dbName = $this->dbName;
  //now connect to the database
  $this->linkID = mysql_pconnect($host, $user, $pwd);
  if (!$this->linkID)
  {
   $this->halt();
   return 0;
  }
  if (!mysql_select_db($dbName, $this->linkID))
  {
   $this->halt();
   return 0;
  }
  return $this->linkID;   
 }
 //======================================
 // 函数: query($sql)
 // 功能: 数据查询
 // 参数: $sql 要查询的SQL语句
 // 返回: 0:失败
 //======================================
 function query($sql)
 {
  $this->queryTimes++;
  $this->queryID = mysql_query($sql, $this->linkID);
  if (!$this->queryID)
  { 
   $this->halt();
   return 0;
  }
  return $this->queryID;
 }
 //======================================
 // 函数: setFetchMode($mode)
 // 功能: 设置取得记录的模式
 // 参数: $mode 模式 MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
 // 返回: 0:失败
 //======================================
 function setFetchMode($mode)
 {
  if ($mode == MYSQL_ASSOC || $mode == MYSQL_NUM || $mode == MYSQL_BOTH)
  {
   $this->fetchMode = $mode;
   return 1;
  }
  else
  {
   $this->halt("错误的模式.");
   return 0;
  }
  
 }
 //======================================
 // 函数: fetchRow()
 // 功能: 从记录集中取出一条记录
 // 返回: 0: 出错 record: 一条记录
 //======================================
 function fetchRow()
 {
  $this->record = mysql_fetch_array($this->queryID,$this->fetchMode);
  return $this->record;
 }
 //======================================
 // 函数: fetchAll()
 // 功能: 从记录集中取出所有记录
 // 返回: 记录集数组
 //======================================
 function fetchAll()
 {
  $arr = array();
  while($this->record = mysql_fetch_array($this->queryID,$this->fetchMode))
  {
   $arr[] = $this->record;
  }
  mysql_free_result($this->queryID);
  return $arr;
 }
 //======================================
 // 函数: getValue()
 // 功能: 返回记录中指定字段的数据
 // 参数: $field 字段名或字段索引
 // 返回: 指定字段的值
 //======================================
 function getValue($field)
 {
  return $this->record[$field];
 }
 //======================================
 // 函数: affectedRows()
 // 功能: 返回影响的记录数
 //======================================
 function affectedRows()
 {
  return mysql_affected_rows($this->linkID);
 }
 //======================================
 // 函数: recordCount()
 // 功能: 返回查询记录的总数
 // 参数: 无
 // 返回: 记录总数
 //======================================
 function recordCount()
 {
  return mysql_num_rows($this->queryID);
 }
 //======================================
 // 函数: getQueryTimes()
 // 功能: 返回查询的次数
 // 参数: 无
 // 返回: 查询的次数
 //======================================
 function getQueryTimes()
 {
  return $this->queryTimes;
 }
 //======================================
 // 函数: getVersion()
 // 功能: 返回mysql的版本
 // 参数: 无
 //======================================
 function getVersion()
 {
  $this->query("select version() as ver");
  $this->fetchRow();
  return $this->getValue("ver");
 }
 //======================================
 // 函数: getDBSize($dbName, $tblPrefix=null)
 // 功能: 返回数据库占用空间大小
 // 参数: $dbName 数据库名
 // 参数: $tblPrefix 表的前缀,可选
 //======================================
 function getDBSize($dbName, $tblPrefix=null)
 {
  $sql = "SHOW TABLE STATUS FROM " . $dbName;
  if($tblPrefix != null) {
   $sql .= " LIKE '$tblPrefix%'";
  }
  $this->query($sql);
  $size = 0;
  while($this->fetchRow())
   $size += $this->getValue("Data_length") + $this->getValue("Index_length");
  return $size;
 }
 //======================================
 // 函数: insertID()
 // 功能: 返回最后一次插入的自增ID
 // 参数: 无
 //======================================
 function insertID() {
  return mysql_insert_id();
 }
 //======================================
 // 函数: halt($err_msg)
 // 功能: 处理所有出错信息
 // 参数: $err_msg 自定义的出错信息
 //=====================================
 function halt($err_msg="")
 {
  if ("" == $err_msg)
  {
   $this->errno = mysql_errno();
   $this->error = mysql_error();
   echo "mysql error:
";
   echo $this->errno.":".$this->error."
";
   exit;
  }
  else
  {
   echo "mysql error:
";
   echo $err_msg."
";
   exit;
  }
 }
}
?>

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境