Source for file db.php
Documentation is available at db.php
* Basic Database Handling System
* GloryLands, a Web-Based, Massive Multiplayer Online RPG/Strategy Game
* Copyright (C) 2008-09 John Haralampidis <jïhnys2[at]gmail.cïm>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For any help/suggestions or troubleshooting you can see the
* project community website at <http://www.glorylands.gr>
* @license GNU/GPLv3 GNU General Public License version 3
* @author John Haralampidis <jïhnys2[at]gmail.cïm>
* @copyright Copyright (C) 2007-2008, John Haralampidis
* Basic database interface
* This class is further extended by the database jet selected. Using this method,
* any kind of database can be used.
* Initialize Database jet, using the configuration provided
* @param array $config The Jet configuration (Structure depends on the Jet)
abstract public function init($config);
* Insert a new data row on the database
* @param string $table The name of the table that will receive the new row
* @param array $data An array that contains the field names (as keys) and the field values to add
* @return bool|resource Returns false in case of error or the resultset of the executed query
abstract public function insert($table, $data);
* Delete a data row from the database
* @param string $table The name of the table that contain the data
* @param array $index_data An array that holds the indexing information of the data being removed (Ex. [name] => 'user')
* @return bool|resource Returns false in case of error or the resultset of the executed query
abstract public function delete($table, $index_data= false);
* Replace a data row into the database
* @param string $table The name of the table that will receive the data
* @param array $data An array that contains the field names and values. If one or more field names are indexing keys, they will define the row that will be updated.
* @return bool|resource Returns false in case of error or the resultset of the executed query
abstract public function replace($table, $data);
* Select a row from the database
* @param string $table The name of the database table that the data resides on
* @param array $index_data [Optional] The indexing fields that define the rows to fetch (ex. [name] => 'user', [password] => '1234')
* @param array $return_data [Optional] The names of the fields that should be returned (if ommited, all of them)
* @param array $order_by [Optional] The name of the sorting field
* @param array $sort_order [Optional] The sort order: 'ASC' for ascending or 'DESC' for descending
* @return bool|resource Returns false in case of error or the resultset of the executed query
abstract public function select($table, $index_data= false, $return_data= false, $order_by= false, $sort_order= false);
* Update a row on the database
* @param string $table The name of the database table that the data resides on
* @param array $index_data The indexing fields that define the row(s) to update (ex. [name] => 'user', [password] => '1234')
* @param array $new_data The new data
* @return bool|resource Returns false in case of error or the resultset of the executed query
abstract public function update($table, $index_data, $new_data);
* Fetch a row from the resultset
* @param int $mode [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
* @param int $resultset [Optional] The resultset to use. If not specified, the last action's resultset will be used.
* @return bool|array Returns false in case of error or the data array if successfull
abstract public function fetch_array($mode= false, $resultset= false);
* Fetch all the rows of the resultset
* @param int $mode [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
* @param int $resultset [Optional] The resultset to use. If not specified, the last action's resultset will be used.
* @return bool|array Returns false in case of error or the data array if successfull
* Fetch a single value from the resultset
* @param int $resultset [Optional] The resultset to use. If not specified, the last action's resultset will be used.
* @return bool|array Returns false in case of error or the data array if successfull
abstract public function fetch_value($resultset= false);
* Get the number of rows in the resultset
* @param int $resultset [Optional] The resultset to use. If not specified, the last action's resultset will be used.
* @return int Returns the number of the rows in the resultset
abstract public function num_rows($resultset= false);
* Get the number of changed rows in the resultset
* @return int Returns the number of the rows that were changed, on the last resultset
* Get the last error message
* @param bool $formatted If TRUE, the message will be properly, HTML formatted
* @return int Returns the HTML or text result
* Get the names of all the tables in the database
* @return bool|array Returns the database tables as array, or FALSE on error
* @return bool|array Returns the table structure as an associative array, with key each field's name
* Manually execute a complex SQL query
* This function should be avoided when possible, because there could be jets that do not support
* @param string $text The query to execute
* @return bool|resource Returns false in case of error or the resultset of the executed query
abstract public function query($text);
* Set the current resultset pointer into a different row
* @param int $position The new position
* @param int $resultset [Optional] The resultset to use. If not specified, the last action's resultset will be used.
* @return bool Returns false in case of error or TRUE if successfull
abstract public function set_row($position, $resultset= false);
* Check if the select query will return data
* @param string $table The name of the database table that the data resides on
* @param array $index_data [Optional] The indexing fields that define the rows to fetch (ex. [name] => 'user', [password] => '1234')
* @param array $return_data [Optional] The names of the fields that should be returned (if ommited, all of them)
* @param array $order_by [Optional] The name of the sorting field
* @param array $sort_order [Optional] The sort order: 'ASC' for ascending or 'DESC' for descending
* @return bool Returns TRUE if data were received or FALSE if the result was empty
public function check_select($table, $index_data= false, $return_data= false, $order_by= false, $sort_order= false) {
$ans = $this->select($table, $index_data, $return_data, $order_by, $sort_order);
if ($this->num_rows($ans) < 1) return false;
* Select data and fetch the first row
* @param string $table The name of the database table that the data resides on
* @param array $index_data [Optional] The indexing fields that define the rows to fetch (ex. [name] => 'user', [password] => '1234')
* @param array $return_data [Optional] The names of the fields that should be returned (if ommited, all of them)
* @param array $order_by [Optional] The name of the sorting field
* @param array $sort_order [Optional] The sort order: 'ASC' for ascending or 'DESC' for descending
* @return bool|array Returns false in case of error or the data array if successfull
public function select_and_get($table, $index_data= false, $return_data= false, $order_by= false, $sort_order= false) {
$resultset = $this->select($table, $index_data, $return_data, $order_by, $sort_order);
* Perform an SQ query and return the first row
* This function should be avoided when possible, because there could be jets that do not support
* @param string $text The query to execute
* @return bool|array Returns false in case of error or the data array if successfull
$resultset = $this->query($text);
* Check if the resultset is empty
* @return bool Returns TRUE if the resultset is empty, or FALSE otherways
* Fetch a row from the resultset using an extended field
* The extended field is a field in the resultset that contains PHP serialized data. Theese
* data will be automatically expanded and inserted in the returning array
* @param int $mode [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
* @param int $resultset [Optional] The resultset to use. If not specified, the last action's resultset will be used.
* @param string $extrafield [Optional] The name of the extra field (defaults to 'extra')
* @return bool|array Returns false in case of error or the data array if successfull
public function fetch_array_ex($mode= false, $resultset= false, $extrafield= 'extra') {
if (isset ($row[$extrafield])) {
if ($row[$extrafield] != '') {
unset ($row[$extrafield]);
* Fetch all the rows from the resultset using an extended field
* The extended field is a field in the resultset that contains PHP serialized data. Theese
* data will be automatically expanded and inserted in the returning array
* @param int $mode [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
* @param int $resultset [Optional] The resultset to use. If not specified, the last action's resultset will be used.
* @param string $extrafield [Optional] The name of the extra field (defaults to 'extra')
* @return bool|array Returns false in case of error or the data array if successfull
foreach ($rows as $id => $row) {
if (isset ($row[$extrafield])) {
if ($row[$extrafield] != '') {
unset ($row[$extrafield]);
* The maximum number of rows per page in the paging system
* Sets to TRUE if the paging procedure is completed
* The total pages in the paging system
* Start the automated data pager
* The paging system allows you to group the results into pages, quickly and efficiently.
* In order to use the paging system, you should first call the paging_start() function and then
* use the paging_fetch_array() function instead of fetch_array()
* @param int $page The active page (Starts from 0)
* @param int $rows_per_page The maximum numbers of rows that will be allowed on each page
* @param int $resultset [Optional] The resultset to use. If not specified, the last action's resultset will be used.
public function paging_start($page, $rows_per_page, $resultset= false) {
$this->paging_max = $rows_per_page;
$this->paging_pages = ceil($count / $rows_per_page);
$start = $page * $rows_per_page;
$this->paging_done = $this->paging_max;
if ($start < 0) $start= 0;
$this->set_row($start, $resultset);
* Get the total ammount of pages that are used by the pager
* @return int The number of pages
return $this->paging_pages;
* Fetch a row from the resultset, using the paging mechanism
* @param int $mode [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
* @param int $resultset [Optional] The resultset to use. If not specified, the last action's resultset will be used.
* @return bool|array Returns false in case of error or when reached the end or the data array if successfull
if ($this->paging_done > $this->paging_max) return false;
* Fetch all the rows of the resultset, using the paging mechanism
* @param int $mode [Optional] The returning array key format. Can be MYSQL_ASSOC for associative array, MYSQL_NUM, for indexed array, or MYSQL_BOTH
* @param int $resultset [Optional] The resultset to use. If not specified, the last action's resultset will be used.
* @return bool|array Returns false in case of error or when reached the end or the data array if successfull
if ($this->paging_done > $this->paging_max) return false;
// Try to load the appropriate jet handlers to run this database
// and initialize the database store
if (is_file(GLConfig::$engine->file('lib/db/jet/db.'. $db['jet']. '.php'))) {
include_once(GLConfig::$engine->file('lib/db/jet/db.'. $db['jet']. '.php'));
$db_name = $db['jet']. '_jet';
GL::$db = new $db_name();
$ans = GL::$db->init($db['config']);
GLError::trigger('Database jet '. $db['jet']. ' could not be initialized!');
|