Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* By Sam Jesso - sam@bike-this.com
- * File: oceansql.php
- * License: GNU GPL.
- * Type: PHP
- * Description: A MySQL (PHP 4 mysql_*) wrapper that makes queries visually easier to follow, execute, and debug. */
- defined("SYSTEM") or die("Access denied.");
- class MySQL
- {
- private $query, $queryData, $whereCount = 0;
- 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(), $type = "AND")
- {
- $this->query.= "INNER JOIN {$table} ";
- if(count($on) > 0)
- {
- $where = "";
- $this->query.= "ON ";
- foreach($on as $key => $value)
- {
- $where.= " {$type} {$key} = {$value}";
- }
- $where = ($type == "AND") ? substr($where, 5)." " : substr($where, 4)." ";
- $this->query.= $where;
- }
- return $this;
- }
- public function where($args = array(), $type = "AND")
- {
- $where = "";
- foreach($args as $key => $value)
- {
- $where.= ($this->whereCount == 0) ? "WHERE {$key} = '{$value}' " : "{$type} {$key} = '{$value}' ";
- $this->whereCount++;
- }
- $this->query.= $where;
- return $this;
- }
- public function search($args = array(), $type = "AND")
- {
- $where = "";
- foreach($args as $key => $value)
- {
- $where.= ($this->whereCount == 0) ? "WHERE {$key} LIKE '%{$value}%' " : "{$type} {$key} LIKE '%{$value}%' ";
- $this->whereCount++;
- }
- $this->query.= $where;
- return $this;
- }
- public function order($by, $type = "DESC")
- {
- if(strtoupper($by) == "RAND")
- {
- $type = "";
- $by = "RAND()";
- }
- else
- {
- $type = ($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->whereCount = 0;
- $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);
- }
- public function debug($html = true) {
- $info = array(
- "Number of queries executed" => $this->numQueries,
- "Current query in queue" => $this->query
- );
- if($html)
- {
- foreach($info as $key => $value)
- echo "<strong>$key:</strong> $value<br />";
- }
- else
- {
- print_r($info);
- }
- }
- public function clear()
- {
- $this->query = "";
- $this->queryData = "";
- $this->whereCount = 0;
- return true;
- }
- }
Add Comment
Please, Sign In to add comment