-
php简单操作mysql数据库的类
这篇文章主要介绍了php简单操作mysql数据库的类,涉及php操作mysql的连接、查询、插入、删除等基本操作方法,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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);
- }
- }
- ?>
出处:http://www.phpfensi.com/php/20210522/15733.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式