DB class for PHP

PHP

Public Domain

Simple DB class for php. Just DB::setPdoObject($pdo) and then DB::query()

Download (right click, save as, rename as appropriate)

Embed

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php

/**
 * Simple static class for easier access to a PDO object. DB makes prepared
 * statements much easier.
 *
 * @package DB
 * @author David Stensland
 */
class DB
{
	/**
	 * Holds either the PDO object or NULL if pdo not connected
	 *
	 * @var null|PDO
	 **/
	static $pdo;

	/**
	 * Holds count of queries done by DB::query()
	 *
	 * @var int
	 **/
	static $qcount = 0;

	/**
	 * Set PDO object to $instance.
	 *
	 * @param PDO $instance Instance of PDO that DB will use.
	 * @return void
	 **/
	public static function setPdoInstance(PDO $instance)
	{
		self::$pdo = $instance;
	}

	/**
	 * Get an instance of the PDO Object. If a PDO object has not been given, 
	 * it this method will throw DB_Exception.
	 *
	 * @throws DB_Exception Thrown if self::$pdo is not a PDO object.
	 * @return PDO
	 */
	public static function getPdoInstance()
	{
		if (self::$pdo instanceof PDO)
			return self::$pdo;
		
		throw new DB_Exception('self::$pdo not set to PDO object.');
	}

	/**
	 * Query functionality is provided through this method. If only $query is
	 * provided, the query will be passed directly to PDO's query method and
	 * returned. If one or more inputs are provided, the query will made a
	 * prepared statement and the $input(s) will be passed to it.
	 *
	 * @throws DB_Exception
	 *
	 * @param mixed  $query		Either query or array containing
	 * @param string $input,...	(Optional) Used as args to $query,if present.
	 *
	 * @return bool|PDOStatement
	 */
	public static function query()
	{
		$args = func_get_args();
		
		if (count($args) == 0)
			throw new DB_Exception('No arguments passed to query.');

		$query = array_shift($args);

		if (count($args) == 0) {
			$args = null;
		}
		
		return self::vquery($query, $args);
	}

	/**
	 * Operates as query but accepts an array of arguments rather than a
	 * variable number of arguments.
	 *
	 * @param string $query The all important SQL query.
	 * @param array  $args  Optional, args to pass if $query should be prepared.
	 * @return bool|PDOStatement
	 **/
	public static function vQuery($query, $args = null)
	{
		if (!is_string($query))
			throw new DB_Exception('Query string not a string.');
		
		if ($args !== null and !is_array($args))
			throw new DB_Exception('Arguments not empty but not array.');
		
		if (!is_array($args)) {
			$r = self::getPdoInstance()->query($query);
			self::$qcount++;
			if ($r === false) {
				$e = self::$pdo->errorInfo();
				throw new DB_Exception($e[2]);
			}
			return $r;
		}
		
		$pdo = self::getPdoInstance();
		$statement = $pdo->prepare($query);
		$statement->execute($args);
		self::$qcount++;
		return $statement;
	}

	private static function _privQuery($in)
	{
		$q = array_shift($in);
		return self::vQuery($q, $in);
	}

	/**
	 * Unset the PDO object, effectively disconnecting.
	 *
	 * @return void
	 */
	public static function disconnect()
	{
		self::$pdo = null;
	}

	/**
	 * Return the first column from the first row.
	 * This is useful for things like SELECT count(...)
	 *
	 * @return bool|string
	 */
	public static function queryI()
	{
		$result = self::_privQuery(func_get_args());
		if (!$result)
			return $result;
		
		$row = $result->fetch(PDO::FETCH_NUM);
		if (isset($row[0])) {
			return $row[0];
		} else {
			return false;
		}
	}

	/**
	 * Preform a query and return all results as associative array.
	 *
	 * @return bool|array
	 */
	public static function queryA($query)
	{
		$result = self::_privQuery(func_get_args());
		
		if (!$result)
			return $result;

		return $result->fetchAll();
	}

	/**
	 * Return the first row of the result as an assoc array
	 *
	 * @return bool|array
	 */
	public static function queryAR()
	{
		$result = self::_privQuery(func_get_args());
		
		if (!$result)
			return $result;

		return $result->fetch(PDO::FETCH_ASSOC);
	}
}

/**
 * Exception for DB errors
 *
 * @package DB
 */
class DB_Exception extends Exception
{
}