<?php/** * MySQL database Operation tool class, facilitate database operation.
* examples See bottom comment.
* @author: http://www.ideawu.net/blog/* * class mysql{var $conn;
var $query _list = Array ();
Public $query _count = 0;
Public function __construct ($c) {if!isset ($c [' Port ']) {$c [' port '] = ' 3306 '; $server = $c [' Host ']. ':' .
$c [' Port '];
$this->conn = mysql_connect ($server, $c [' username '], $c [' Password '], true) or Die (' Connect db error ');
mysql_select_db ($c [' dbname '], $this->conn) or Die (' Select DB error '); if ($c [' CharSet ']) {mysql_query ("set Names").
$c [' CharSet '], $this->conn);
}/** * Executes mysql_query and returns its results.
*/Public Function query ($sql) {$stime = Microtime (true);
$result = mysql_query ($sql, $this->conn);
$this->query_count + +;
if ($result = = False) {throw new Exception mysql_error ($this->conn). "In SQL: $sql"); } $etime = MICROtime (TRUE);
$time = Number_format (($etime-$stime) * 1000, 2); $this->query_list[] = $time. ' ' .
$sql;
return $result;
/** * Executes the SQL statement, returning the first record of the result (is an object).
*/Public function get ($sql) {$result = $this->query ($sql);
if ($row = Mysql_fetch_object ($result)) {return $row;
}else{return null;
}/** * Returns the query result set, with key as the key to the associative array, each element is an object.
* If the key is empty, the result is organized into a normal array.
* * Public Function find ($sql, $key =null) {$data = array ();
$result = $this->query ($sql);
while ($row = Mysql_fetch_object ($result)) {if (!empty ($key)) {$data [$row->{$key}] = $row;
}else{$data [] = $row;
} return $data;
The Public Function last_insert_id () {return mysql_insert_id ($this->conn);
/** * Executes a count SQL statement with the result set count and returns the count.
*/Public function count ($sql) {$result = $this->query ($sql);
if ($row = mysql_fetch_array ($result)) {return (int) $row [0];
}else{return 0;
}/** * begins a transaction.
*/Public Function begin () {mysql_query (' begin ');
}/** * commits a transaction.
* * Public Function commit () {mysql_query (' commit ');
/** * rolls back a transaction.
* * Public Function rollback () {mysql_query (' rollback ');
/** * Gets the record for the specified number.
* @param int $id The number of the record to get.
* @param string $field field name, default to ' ID '.
*/function Load ($table, $id, $field = ' id ') {$sql = "select * from ' {$table} ' where ' {$field} ' = ' {$id} '";
$row = $this->get ($sql);
return $row;
/** * Save a record, the ID is set after the call.
* @param object $row/function Save ($table, & $row) {$sqlA = ';
foreach ($row as $k => $v) {$sqlA. = "' $k ' = ' $v '";
} $sqlA = substr ($sqlA, 0, strlen ($sqlA)-1);
$sql = "INSERT INTO ' {$table} ' set $sqlA";
$this->query ($sql);
if (Is_object ($row)) {$row->id = $this->last_insert_id ();
}else if (Is_array ($row)) {$row [' id '] = $this->last_insert_id ();
}}/** the record specified by the * update $arr[id].
* @param array $row The record to be updated, and the value of the array entry with the ID of the key indicates the record to be updated.
* @return The number of rows affected by int.
* @param string $field field name, default to ' ID '.
*/function Update ($table, & $row, $field = ' id ') {$sqlA = ';
foreach ($row as $k => $v) {$sqlA. = "' $k ' = ' $v '";
$sqlA = substr ($sqlA, 0, strlen ($sqlA)-1);
if (Is_object ($row)) {$id = $row->{$field};
}else if (Is_array ($row)) {$id = $row [$field];
$sql = "Update ' {$table} ' set $sqlA where ' {$field} ' = ' $id '";
return $this->query ($sql);
/** * Deletes a record. * @param int $id to deleteThe record number.
* @return The number of rows affected by int.
* @param string $field field name, default to ' ID '.
*/function Remove ($table, $id, $field = ' id ') {$sql = ' delete from ' {$table} ' where ' {$field} ' = ' {$id} ' ';
return $this->query ($sql);
Function Escape (& $val) {if (Is_object ($val) Is_array ($val)) {$this->escape_row ($val);
} function Escape_row (& $row) {if (Is_object ($row)) {foreach ($row as $k => $v) {
$row-> $k = mysql_real_escape_string ($v); }else if (Is_array ($row)) {foreach ($row as $k => $v) {$row [$k] = mysql_real_escape_s
Tring ($v);
}} function Escape_like_string ($str) {$find = array ('% ', ' _ ');
$replace = Array (' \% ', ' \_ ');
$str = Str_replace ($find, $replace, $STR);
return $str;
}}?>
Use examples:
<?php
//Save
$db->save (' table_1 ', $row);
Update
$db->update (' table_1 ', $row);
Delete
$db->remove (' Table_1 ', 1);
Query
$rows = $db->find ($sql, ' id ')
?>