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 相关文章推荐
PHP4和PHP5性能测试和对比 测试代码与环境
Aug 17 PHP
php xfocus防注入资料
Apr 27 PHP
IIS php环境配置PHP5 MySQL5 ZendOptimizer phpmyadmin安装与配置
Nov 18 PHP
Memcache 在PHP中的使用技巧
Feb 08 PHP
利用PHP实现智能文件类型检测的实现代码
Aug 02 PHP
如何用phpmyadmin设置mysql数据库用户的权限
Jan 09 PHP
PHP多线程批量采集下载美女图片的实现代码(续)
Jun 03 PHP
PHP验证码函数代码(简单实用)
Sep 29 PHP
ThinkPHP之N方法实例详解
Jun 20 PHP
详解WordPress中调用评论模板和循环输出评论的PHP函数
Jan 05 PHP
浅谈PHP中pack、unpack的详细用法
Mar 12 PHP
Laravel框架控制器的middleware中间件用法分析
Sep 30 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
杏林同学录(二)
2006/10/09 PHP
php 保留小数点
2009/04/21 PHP
PHP错误Allowed memory size of 67108864 bytes exhausted的3种解决办法
2014/07/28 PHP
详解WordPress开发中get_header()获取头部函数的用法
2016/01/08 PHP
如何解决Jquery库及其他库之间的$命名冲突
2013/09/15 Javascript
jquery中的ajax方法怎样通过JSONP进行远程调用
2014/05/04 Javascript
使用微信内置浏览器点击下拉框出现页面乱跳转现象(iphone),该怎么办
2016/01/04 Javascript
JavaScript开发者必备的10个Sublime Text插件
2016/02/27 Javascript
解读Bootstrap v4 sass设计
2016/05/29 Javascript
js数组操作方法总结(必看篇)
2016/11/22 Javascript
jQuery ajax请求struts action实现异步刷新
2017/04/19 jQuery
vue单页面打包文件大?首次加载慢?nginx带你飞,从7.5M到1.3M蜕变过程(推荐)
2018/01/16 Javascript
Angular resolve基础用法详解
2018/10/03 Javascript
vue-cli项目中使用echarts图表实例
2018/10/22 Javascript
基于vue实现移动端圆形旋钮插件效果
2018/11/28 Javascript
vue工程全局设置ajax的等待动效的方法
2019/02/22 Javascript
微信小程序模板消息限制实现无限制主动推送的示例代码
2019/08/27 Javascript
jQuery模仿ToDoList实现简单的待办事项列表
2019/12/30 jQuery
node.js 微信开发之定时获取access_token
2020/02/07 Javascript
javascript事件循环event loop的简单模型解释与应用分析
2020/03/14 Javascript
js中!和!!的区别与用法
2020/05/09 Javascript
浅析使用Python操作文件
2017/07/31 Python
Python实现简单http服务器
2018/04/12 Python
Python用61行代码实现图片像素化的示例代码
2018/12/10 Python
python 搜索大文件的实例代码
2019/07/08 Python
使用 Python 写一个简易的抽奖程序
2019/12/08 Python
使用OpenCV对车道进行实时检测的实现示例代码
2020/06/19 Python
比利时网上药店: Drogisterij.net
2017/03/17 全球购物
一个J2EE项目团队的主要人员组成是什么
2012/06/04 面试题
中学教师管理制度
2014/01/14 职场文书
施工协议书范本
2014/04/22 职场文书
给校长的建议书200字
2014/05/16 职场文书
领导干部廉政自律承诺书
2014/05/26 职场文书
材料专业大学毕业生自荐书
2014/07/02 职场文书
导游词之上海杜莎夫人蜡像馆
2019/11/22 职场文书
SpringBoot集成MongoDB实现文件上传的步骤
2022/04/18 MongoDB