The examples in this article describe the PHP simple operation of the MySQL database class. Share to everyone for your reference. as follows:
<?php/** * Database class * * @version: 2.2 * @revised: 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];
The 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 = FAL
SE) {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);
The 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; The 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;
The 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; The function Getcells ($table) {$query = "show COLUMNS from". $table. "
`";
$fields = mysql_query ($query, $this->linkid) or Die (' Hej ');
return $fields; The 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); }}?>
I hope this article will help you with your Php+mysql database program.