Source for file Database.php
Documentation is available at Database.php
* MySQL abstraction layer
* @see DatabaseConnection
* @copyright 2009-2010 NADEO
* require_once(APP_FRAMEWORK_LIBRARIES_PATH.'Database.php');
* $database = DatabaseConnection::getInstance();
* $result = $database->execute('SELECT * FROM mytable WHERE id < 10');
* while($array = $result->fetchAssoc())
* $myvar = 'Some \'text with quotes\' and "double quotes"';
* $myvarQuoted = $database->quote($myvar);
* $database->execute( 'INSERT INTO mytable (MyText) VALUES ('.$myvarQuoted.')' );
* echo $database->insertID.' is a newly inserted ID';
static protected $instance;
* Get an instance on the database connection object, and connects to the mysql server if needed
* To configure your connection, override these constants in your config:
* <li>APP_DATABASE_HOST</li>
* <li>APP_DATABASE_USER</li>
* <li>APP_DATABASE_PASSWORD</li>
* <li>APP_DATABASE_NAME</li>
* <li>APP_DATABASE_CHARSET</li>
* @return DatabaseConnection
self::$instance =
new self();
* @throws DatabaseConnectionException
protected function __construct()
$this->host =
APP_DATABASE_HOST;
$this->connection =
mysql_connect($this->host, $this->user, $this->password);
throw
new DatabaseConnectionException();
* Sets the charset for the database connection
if($charset !=
$this->charset)
$this->charset =
$charset;
throw
new DatabaseException('Couldn\'t set charset: '.
$charset);
if($database !=
$this->database)
$this->database =
$database;
* Escape and quote variables so you can insert them safely
* @param string The query
* @return DatabaseRecordSet
* Get number of affected rows in previous operation
* Get the ID generated in the last query
return (!$this->connection);
* Currently selected database
* @see DatabaseConnection
* Constants to use with DatabaseRecordSet::fetchArray()
const FETCH_ASSOC =
MYSQL_ASSOC;
const FETCH_NUM =
MYSQL_NUM;
const FETCH_BOTH =
MYSQL_BOTH;
function __construct($result)
* Get a result row as an enumerated array
* Fetch a result row as an associative array
* Fetch a result row as an associative, a numeric array, or both
function fetchArray($resultType =
self::FETCH_ASSOC)
* Returns the current row of a result set as an object
* @param string The name of the class to instantiate, set the properties of and return. If not specified, a stdClass object is returned.
* @param array An optional array of parameters to pass to the constructor for class_name objects.
function fetchObject($className, array $params =
array())
* Gets the number of rows in a result
* @see DatabaseConnection
* Returns the "LIMIT x,x" string depending on both values
elseif(!$offset &&
$length==
1)
return 'LIMIT '.
$offset.
', '.
$length;
* Returns string like "(name1, name2) VALUES (value1, value2)"
'('.
implode(', ', array_keys($values)).
') '.
'('.
implode(', ', $values).
')';
* Returns string like "name1=VALUES(name1), name2=VALUES(name2)"
foreach($valueNames as $valueName)
$strings[] =
$valueName.
'=VALUES('.
$valueName.
')';
class DatabaseException extends Exception {}
class DatabaseConnectionException extends DatabaseException {}
class DatabaseSelectionException extends DatabaseException {}
class DatabaseQueryException extends DatabaseException {}