Created
December 20, 2010 01:27
-
-
Save jamsesso/747912 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* #!modules/mysql.php | |
* Bike-This.com - Modules | |
* By Sam Jesso - [email protected] | |
* File: mysql.php | |
* Type: PHP | |
* Description: A MySQL (PHP 4 mysql_*) wrapper that makes queries visually easier to follow, execute, and debug. | |
*/ | |
class MySQL | |
{ | |
private $query, $queryData; | |
public $numQueries; | |
public function __construct($user, $pass, $db, $host = "localhost") | |
{ | |
$conn = mysql_connect($host, $user, $pass) or die(mysql_error()); | |
mysql_select_db($db, $conn) or die(mysql_error()); | |
return $this; | |
} | |
public function __destruct() | |
{ | |
mysql_close(); | |
} | |
public function __toString() | |
{ | |
return (string) $this->numQueries; | |
} | |
public function select($from, $fields = array()) | |
{ | |
if(count($fields) > 0) | |
{ | |
$fieldstr = ""; | |
foreach($fields as $key => $value) | |
$fieldstr.= (is_numeric($key)) ? ", {$value}" : ", {$key} AS {$value}"; | |
$fieldstr = substr($fieldstr, 2); | |
} | |
else | |
{ | |
$fieldstr = "*"; | |
} | |
$this->query.= "SELECT {$fieldstr} FROM {$from} "; | |
return $this; | |
} | |
public function query($query) | |
{ | |
$this->queryData = mysql_query($query) or die(mysql_error()); | |
$this->numQueries++; | |
return $this; | |
} | |
public function rawquery($query) | |
{ | |
$this->numQueries++; | |
return mysql_query($query); | |
} | |
public function update($table) | |
{ | |
$this->query.= "UPDATE {$table} "; | |
return $this; | |
} | |
public function set($settings = array()) | |
{ | |
$this->query.= "SET "; | |
$set = ""; | |
foreach($settings as $key => $value) | |
$set.= ", {$key} = '{$value}'"; | |
$set = substr($set, 2); | |
$this->query.= $set." "; | |
return $this; | |
} | |
public function delete($from) | |
{ | |
$this->query.= "DELETE FROM {$from} "; | |
return $this; | |
} | |
public function insert($into) | |
{ | |
$this->query.= "INSERT INTO {$into} "; | |
return $this; | |
} | |
public function values($array) | |
{ | |
$keys = ""; | |
$values = ""; | |
foreach($array as $key => $value) | |
{ | |
$keys.= ", {$key}"; | |
$values.= ", '{$value}'"; | |
} | |
$keys = substr($keys, 2); | |
$values = substr($values, 2); | |
$this->query.= "({$keys}) VALUES ({$values}) "; | |
return $this; | |
} | |
public function join($table, $on = array()) | |
{ | |
$this->query.= "INNER JOIN {$table} "; | |
if(count($on) > 0) | |
{ | |
$where = ""; | |
$this->query.= "ON "; | |
foreach($on as $key => $value) | |
{ | |
$where.= ", {$key} = {$value}"; | |
} | |
$where = substr($where, 2)." "; | |
$this->query.= $where; | |
} | |
return $this; | |
} | |
public function where($args = array(), $type = "AND") | |
{ | |
$i = 0; | |
$where = ""; | |
foreach($args as $key => $value) | |
{ | |
$where.= ($i == 0) ? "WHERE {$key} = '{$value}' " : "{$type} {$key} = '{$value}' "; | |
$i++; | |
} | |
$this->query.= $where; | |
return $this; | |
} | |
public function order($by, $type = "DESC") | |
{ | |
if(strtoupper($by) == "RAND") | |
{ | |
$by = "RAND()"; | |
$type = ""; | |
} | |
else | |
{ | |
$type = (strtoupper($type) == "DESC") ? " DESC" : " ASC"; | |
} | |
$this->query.= "ORDER BY {$by}{$type} "; | |
return $this; | |
} | |
public function limit($start, $finish = NULL) | |
{ | |
$limit = "LIMIT {$start} "; | |
if($finish) | |
$limit.= substr($limit, 0, -1).",{$finish} "; | |
$this->query.= $limit; | |
return $this; | |
} | |
public function execute() | |
{ | |
$this->numQueries++; | |
$this->query = substr($this->query, 0, -1); | |
$this->queryData = mysql_query($this->query) or die(mysql_error()); | |
$this->query = ""; | |
return $this; | |
} | |
public function fetch($as = "object") | |
{ | |
return ($as == "object") ? mysql_fetch_object($this->queryData) : mysql_fetch_array($this->queryData); | |
} | |
public function results() | |
{ | |
return mysql_num_rows($this->queryData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment