Here is a MySQL library I wrote a while back. Enjoy.
PHP Code:
<?php
error_reporting(E_ALL);
/*----------------------------------------------------------------*\
Rival Webs (http://www.rivalwebs.net Version 0.0.9
----------------------------------------------------------------
MySQL Database Driver (Rev: 5-20-2007)
Copyright (c) 2004-2005 Scott Bedard.
\*----------------------------------------------------------------*/
/**
* MySQL Database Driver
* @author Scott Bedard
* @copyright Copyright © 2004-2006 Scott Bedard.
* @version Revision: 5-20-2007
*/
// microtime
function start_clock() {
global $query_time, $query_count, $time_start;
$time_start = microtime(true);
$query_count++;
}
function stop_clock() {
global $query_time, $query_count, $time_start;
$time_end = microtime(true);
$time = $time_end - $time_start;
$query_time = round($query_time + $time, 4);
}
// sql_connect()
function sql_connect($server= null, $database= null, $username= null, $password= null) {
if (is_null($server) || is_null($database) || is_null($username) || is_null($password)) {
return false;
}
start_clock();
$connect = mysql_connect($server, $username, $password);
stop_clock();
start_clock();
$db = mysql_select_db($database);
stop_clock();
if (!$connect) {
return false;
} else {
return true;
}
}
// sql_return_connect()
function sql_connect_return($sever= null, $database = null, $username= null, $password=null) {
if (is_null($server) || is_null($database) || is_null($username) || is_null($password)) {
return false;
}
$connect_return = "mysql_connect('".$server."', '".$username."', '".$password."'):";
$db_return = "mysql_select_db('".$database."');";
return $connect_return." ".$db_return;
}
// sql_select()
function sql_select($table= null, $field= null, $condition= null) {
if (is_null($table) || is_null($field) || is_null($condition)) {
return false;
}
$i = 0;
$condition_string = '';
$last = count($condition) - 1;
foreach ($condition as $a => $b) {
$condition_string .= "`".$a."` = '".$b."'";
if ($i < $last)
$condition_string .= "AND ";
$i++;
}
if ($field == '*') {
$field_string = '*';
} else {
$field_string = "`".$field."`";
}
$query = "SELECT ".$field_string." from `".$table."` WHERE ".$condition_string;
start_clock();
$sql = mysql_query($query);
stop_clock();
if (!$sql) {
return false;
} else {
return mysql_fetch_array($sql);
}
}
// sql_select_all()
function sql_select_all($table= null, $field= null, $sort= null){
if(is_null($table)){
return false;
}
if(is_null($sort) || is_null($field)){
$query = "SELECT * from `".$table."`";
} else {
$query = "SELECT * from `".$table."` ORDER BY `".$field."` ".$sort;
}
start_clock();
$sql = mysql_query($query);
stop_clock();
while($row = mysql_fetch_array($sql)){
$return[] = $row;
}
if(empty($return))
return false;
else
return $return;
}
// sql_select_array()
function sql_select_array($table = null, $field = null, $condition = null,
$order = null, $key = null)
{
if (is_null($table)) {
return false;
}
$query = 'SELECT ';
if (is_null($field)) {
$query .= '* ';
} else if (is_string($field)) {
$query .= $field.' ';
} else if (is_array($field)) {
$query .= implode(', ', $field);
}
$query .= 'FROM '.$table;
if (is_string($condition)) {
$query .= ' WHERE '.$condition;
} else {
$query .= ' WHERE ';
$conds = array();
foreach ($condition as $col => $val) {
if (is_array($val)) {
$conds[] = $col.' IN ('.implode(', ', array_map('_quote', $val)).
')';
} else {
$conds[] = $col.' = \''.$val.'\'';
}
$query .= implode(' AND ', $conds);
}
}
if (!is_null($order)) {
if (is_string($order)) {
$matches = array();
$mcnt = preg_match_all('/(\w+)([+-])/', $order, $matches,
PREG_SET_ORDER);
$order = array();
foreach ($matches as $m) {
$order[$m[1]] = ($m[2] == '+') ? 'ASC' : 'DESC';
}
}
$o = array();
foreach ($order as $col => $dir) {
switch (strtolower($dir{0})) {
case 'a':
case '+':
$o[] = $col.' ASC';
break;
case 'd':
case '-':
$o[] = $col.' DESC';
break;
}
}
$query .= ' ORDER BY '.implode(', ', $o);
}
start_clock();
$reply = mysql_query($query);
stop_clock();
if (!$reply) {
return false;
}
if (mysql_num_rows($reply) == 0) {
return array();
}
$ret = array();
while ($row = mysql_fetch_assoc($reply)) {
if ($key) {
$ret[$row[$key]] = $row;
} else {
$ret[] = $row;
}
}
return $ret;
}
// sql_insert()
function sql_insert($table = null, $values = null) {
if (is_null($table) || is_null($values)) {
return false;
}
$columns_specified = false;
foreach ($values as $k => $v) {
if (!$columns_specified && is_string($k))
$columns_specified = true;
if (is_null($v))
$values[$k] = 'NULL';
else
$values[$k] = '\''.mysql_real_escape_string($v).'\'';
}
$value_str = implode(', ', $values);
$query = 'INSERT INTO '.$table.' ';
if ($columns_specified) {
$query .= '('.implode(', ', array_keys($values)).') ';
}
$query .= 'VALUES ('.$value_str.')';
start_clock();
$result = mysql_query($query);
stop_clock();
if (!$result) {
return false;
} else {
return (($id = mysql_insert_id()) === 0) ? true : $id;
}
}
// sql_update()
function sql_update($table= null, $fields= null, $condition= null, $limit= null) {
if (is_null($table) || is_null($fields) || is_null($condition)) {
return false;
}
if (is_null($limit)) {
$limit = '1';
}
$i = 0;
$field_string = '';
$last = count($fields) - 1;
foreach ($fields as $a => $b) {
$field_string .= "`".$a."`='".$b."'";
if ($i < $last) {
$field_string .= ', ';
}
$i++;
}
$i = 0;
$condition_string = '';
$last = count($condition) - 1;
foreach ($condition as $a => $b) {
$condition_string .= "`".$a."`='".$b."'";
if ($i < $last) {
$condition_string .= 'AND ';
}
$i++;
}
$query = "UPDATE `".$table."` SET ".$field_string." WHERE ".$condition_string." LIMIT 1";
start_clock();
$sql = mysql_query($query);
stop_clock();
if (!$sql) {
return false;
} else {
return true;
}
}
// sql_delete()
function sql_delete($table = null, $condition = null) {
if (is_null($table) || is_null($condition)) {
return false;
}
$i = 0;
$condition_string = '';
$last = count($condition) - 1;
foreach ($condition as $a => $b) {
$condition_string .= "`".$a."`='".$b."'";
if ($i < $last)
$condition_string .= 'AND ';
$i++;
}
$query = 'DELETE FROM `'.$table.'` WHERE '.$condition_string;
start_clock();
$sql = mysql_query($query);
stop_clock();
if (!$sql) {
return false;
} else {
return true;
}
}
// sql_count()
function sql_count($table = null) {
if (is_null($table)) {
return 0;
} else {
start_clock();
$query = mysql_query("SELECT * FROM ".$table);
$rows = mysql_num_rows($query);
stop_clock();
return $rows;
}
}
?>