php简单操作mysql数据库的类


Posted in PHP onApril 16, 2015

本文实例讲述了php简单操作mysql数据库的类。分享给大家供大家参考。具体如下:

<?php
/**
 * Database class
 *
 * @version: 2.2
 * @revised: 27 may 2007
 *
 **/
class Database {
 var $host;
 var $name;
 var $user;
 var $pass;
 var $prefix;
 var $linkId;
 function Database($mysql) {
  foreach($mysql as $k => $v){
   $this->$k = $v;
  }
  if(strlen($this->prefix)>0 && substr($this->prefix, -1) !== "_")
  $prefix .= "_";
  $this->prefix = $prefix;
 }
 function getLastID() {
  $id = mysql_fetch_row(mysql_query("SELECT LAST_INSERT_ID()", $this->linkId));
  return $id[0];
 }
 function getPossibleValues($tableA, $whereA) {
  if(gettype($tableA) == "array") {
   $table = "";
   foreach($tableA as $t) {
    $table .= $this->prefix.$t.", ";
   }
   $table = substr($table, 0, -2);
  } else $table = $this->prefix.$tableA;
  if(gettype($whereA) != "array") {
   $whereA = array($whereA);
  }
  $return = array();
  foreach($whereA as $where) {
   $sql = mysql_query("SHOW COLUMNS FROM ".$table." LIKE '%".$where."%'");
   while($arr = mysql_fetch_array($sql)) {
    if(strpos($arr['Type'], 'enum')===0) {
     $vals = substr($arr['Type'], 5, -1);
    } else {
     $vals = substr($arr['Type'], 4, -1);
    }
    $vals = str_replace("'","",$vals);
    $vals = explode(",",$vals);
    $i = 1;
    foreach($vals as $val) {
     $return[$arr['Field']][$i++] = $val;
    }
    $return[$arr['Field']]['default'] = $arr['Default'];
    if($arr['Null'] != "NO") $return[$arr['Field']][0] = NULL;
   }
  }
  return $return;
 }
 function connect() {
  $this->linkId = mysql_connect($this->host, $this->user, $this->pass);
  if(!$this->linkId) {
   return false;
  }
  if(mysql_select_db($this->name, $this->linkId)) return true;
  mysql_close($this->linkId);
  return false;
 }
 function runSelect($tables, $where = "1", $fieldsA = "*", $order = false, $limit = false, $offset = false, $group = false) {
  if(gettype($tables) == "array") {
   $table = "";
   foreach($tables as $t) {
    $table .= $this->prefix.$t.", ";
   }
   $table = substr($table, 0, -2);
  } else $table = $this->prefix.$tables;
  if(gettype($fieldsA) == "array") {
   $fields = "";
   $keys = array_keys($fieldsA);
   if($keys[0] != '0') {
    foreach($keys as $key) {
     $fields .= $key.' AS '.$fieldsA[$key].', ';
    }
   } else {
    foreach($fieldsA as $field) {
     $fields .= $field.', ';
    }
   }
   $fields = substr($fields, 0, -2);
  } else $fields = $fieldsA;
  $query = "SELECT ".$fields." FROM ".$table." WHERE ".$where.
   ($order!== false?" ORDER BY ".$order:($group!==false ? " GROUP BY ".$group : "")).
   ($limit !== false?" LIMIT ".$limit:"").
   ($offset !== false?" OFFSET ".$offset:"");
  return mysql_query($query, $this->linkId);
 }
 function runUpdate($table, $valuesA, $where = "1") {
  if(gettype($valuesA) == "array") {
   $fields = "";
   $values = "";
   $keys = array_keys($valuesA);
   foreach($keys as $key) {
    if($valuesA[$key] !== NULL)
     $values .= "`".$key."`='".str_replace("'",'\'', $valuesA[$key])."',";
    else
     $values .= $key."=NULL,";
   }
   $fields = substr($fields, 0, -1);
   $values = substr($values, 0, -1);
  } else $values = $valuesA;
  $query = "UPDATE ".$this->prefix.$table." SET ".$values." WHERE ".$where;
  if(mysql_query($query,
    $this->linkId))
   return mysql_affected_rows($this->linkId);
  return false;
 }
 function runDelete($table, $where = "1") {
  if(mysql_query("DELETE FROM ".$this->prefix.$table." WHERE ".$where, $this->linkId))
   return mysql_affected_rows($this->linkId);
  return false;
 }
 function runInsert($table, $valuesA, $onDuplicate = NULL) {
  if(gettype($valuesA) == "array") {
   $fields = "";
   $values = "";
   $keys = array_keys($valuesA);
   foreach($keys as $key) {
    $fields .= "`".$key."`, ";
    $values .= ($valuesA[$key]===NULL?"NULL, ":"'".str_replace("'", '\'', $valuesA[$key])."', ");
   }
   $fields = substr($fields, 0, -2);
   $values = substr($values, 0, -2);
  }
  $onDup = "";
  if($onDuplicate != NULL) {
   $onDup = " ON DUPLICATE KEY UPDATE ";
   if(gettype($onDuplicate) == "array") {
    $keys = array_keys($onDuplicate);
    foreach($keys as $key) {
     $onDup .= '`'.$key.'`='.($onDuplicate[$key]===NULL?"NULL,":"'".str_replace("'", '\'', $onDuplicate[$key])."', ");
    }
    $onDup = substr($onDup, 0, -2);
   } else $onDup .= $onDuplicate;
  }
  $query = "INSERT INTO ".$this->prefix.$table.($fields!==NULL?"(".$fields.")":"").
   " VALUES (".$values.")".$onDup;
  if(mysql_query($query, $this->linkId))
   return mysql_affected_rows($this->linkId);
  return false;
 }
 function getCells($table){
  $query = "SHOW COLUMNS FROM `".$table."`";
  $fields = mysql_query($query, $this->linkId) or die('hej');
  return $fields;
 }
 function translateCellName($cellName){
  $sql = $this->runSelect("mysql_cell_translation","mysql_name = '".$cellName."'");
  $row = mysql_fetch_assoc($sql);
  return $row['human_name']?$row['human_name']:'<span class="faded">['.$cellName.']</span>';
 }
 function getError() {
  return mysql_error($this->linkId);
 }
 function close()
 {
  mysql_close($this->linkId);
 }
}
?>

希望本文所述对大家的php+mysql数据库程序设计有所帮助。

PHP 相关文章推荐
dedecms中显示数字验证码的修改方法
Mar 21 PHP
基于MySQL分区性能的详细介绍
May 02 PHP
淘宝ip地址查询类分享(利用淘宝ip库)
Jan 07 PHP
实现获取http内容的php函数分享
Feb 16 PHP
ThinkPHP3.1新特性之Action参数绑定
Jun 19 PHP
PHP获取客户端真实IP地址的5种情况分析和实现代码
Jul 08 PHP
php.ini save_handler 修改不生效的解决办法
Jul 22 PHP
Yii框架中memcache用法实例
Dec 03 PHP
thinkPHP实现多字段模糊匹配查询的方法
Dec 01 PHP
PHP会话控制实例分析
Dec 24 PHP
php获取ip及网址的简单方法(必看)
Apr 01 PHP
PHP 二维array转换json的实例讲解
Aug 21 PHP
PHP扩展程序实现守护进程
Apr 16 #PHP
php简单实现查询数据库返回json数据
Apr 16 #PHP
PHP中SESSION的注销与清除
Apr 16 #PHP
PHP中trim()函数简单使用指南
Apr 16 #PHP
php检查是否是ajax请求的方法
Apr 16 #PHP
php将日期格式转换成xx天前的格式
Apr 16 #PHP
为百度UE编辑器上传图片添加水印功能
Apr 16 #PHP
You might like
PHP 冒泡排序 二分查找 顺序查找 二维数组排序算法函数的详解
2013/06/25 PHP
php导出excel格式数据问题
2014/03/11 PHP
PHP中文编码小技巧
2014/12/25 PHP
php获取百度收录、百度热词及百度快照的方法
2015/04/02 PHP
php设置页面超时时间解决方法
2015/09/22 PHP
PHP的APC模块实现上传进度条
2015/10/27 PHP
laravel框架关于搜索功能的实现
2018/03/15 PHP
php实现的证件照换底色功能示例【人像抠图/换背景图】
2020/05/29 PHP
jquery 插件之仿“卓越亚马逊”首页弹出菜单效果
2008/12/25 Javascript
IFrame跨域高度自适应实现代码
2012/08/16 Javascript
angularjs学习笔记之简单介绍
2015/09/26 Javascript
详解Js模板引擎(TrimPath)
2016/11/22 Javascript
12 款 JS 代码测试必备工具(翻译)
2016/12/13 Javascript
jQuery插件HighCharts绘制2D柱状图、折线图和饼图的组合图效果示例【附demo源码下载】
2017/03/09 Javascript
微信小程序实现全国机场索引列表
2018/01/31 Javascript
使用koa-log4管理nodeJs日志笔记的使用方法
2018/11/30 NodeJs
Promise扫盲贴
2019/06/24 Javascript
ligerUI的ligerDialog关闭刷新的方法
2019/09/27 Javascript
node.js Promise对象的使用方法实例分析
2019/12/26 Javascript
使用python编写监听端
2018/04/12 Python
Python二叉搜索树与双向链表转换算法示例
2019/03/02 Python
Python任务调度利器之APScheduler详解
2020/04/02 Python
Python Selenium截图功能实现代码
2020/04/26 Python
Python 实现3种回归模型(Linear Regression,Lasso,Ridge)的示例
2020/10/15 Python
Python实现一个论文下载器的过程
2021/01/18 Python
Python 中如何使用 virtualenv 管理虚拟环境
2021/01/21 Python
用html5的canvas画布绘制贝塞尔曲线完整代码
2013/08/14 HTML / CSS
GafasWorld哥伦比亚:网上购买眼镜
2017/11/28 全球购物
Rowdy Gentleman服装和配饰:美好时光
2019/09/24 全球购物
Craghoppers德国官网:户外和旅行服装
2020/02/14 全球购物
婚礼证婚人演讲稿
2014/09/13 职场文书
2014年会计人员工作总结
2014/12/10 职场文书
导游词欢迎词
2015/02/02 职场文书
党员考试作弊检讨书1000字
2015/02/16 职场文书
幼儿园六一主持词开场白
2015/05/28 职场文书
第一军规观后感
2015/06/12 职场文书