Source for file db.mysql.php
Documentation is available at db.mysql.php
* 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
* Connection ID Definition
* Description of current action in case of error
* Last query's affected rows
* Last query's number of rows
if ($resultset === false) {
* Last query didn't return any results
* The number of queries performed
* The total time spent on queries
* If we have global debug enabled, this function will store the
* Initializes MySQL Class
* @param string $vdb Database name
* @param string $vhost Database server host
* @param string $vuser Login user name
* @param string $vpwd Login user password
* @param bool $presistent Make a presitent connection with the server
// Get variables from the config
$vdb = $config['DATABASE'];
$vhost = $config['HOST'];
$vuser = $config['USER'];
$vpwd = $config['PASSWORD'];
if (isset ($config['PRESISTENT'])) $presistent = $config['PRESISTENT'];
$this->errPosition = "selecting database '<strong>{$vdb}</strong>'";
mysql_query("SET SESSION collation_connection ='utf8_general_ci'");
* @param string $text Query text to execute
* @return bool|resourceReturns the query resultset or false in case of error
$this->errPosition = "performing query '<strong>{$text}</strong>'";
//debug_error($this->get_error_message(),ERR_WARNING);
* Return the first value of the first row of the requested query, using a specified resultset
* @param resource $result (optional) Resultset obdained from a query() or query-like function
* @return bool|resourceReturns the value or false in case of error
* Release a specific query resultset
* @param resource $resultset Resultset obdained from query() function
* Return a row from the last queried resultset
* @param int $resmode The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH
* @return bool|resourceReturns the row or false in case of error or end of results
function fetch_array($resmode = MYSQL_BOTH, $resultset = false) {
if (!$resultset) return false;
* Return all the rows from the last queried resultset
* @param int $resmode The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH
* @return bool|resourceReturns the row or false in case of error or end of results
if (!$resultset) return array();
* Release the last queried resultset
* Moves the internal row pointer to a new position
* @param int $row The row index to jump to
function set_row($row, $resultset= false) {
* Returns detailed information for the last error occured
* @param bool $formatted (optional) TRUE If you want the result to be a pre-formatted HTML response
* @return string An HTML-Formatted error description
return "<font face=Arial size=1 color=red>MySQL error while " . $this->errPosition . " : <font color=blue>" . mysql_error() . "</font></font>";
* Insert a new row on specified table
* @param string $table The table name to add the data
* @param array $data An one-dimensional 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
function insert($table, $data) {
foreach ($data as $name => $value) {
if ($vars != "") $vars .= ", ";
if ($vals != "") $vals .= ", ";
return $this->query("INSERT INTO `{$table}` ({$vars}) VALUES ({$vals})");
* Replace or insert a new row on specified table
* @param string $table The table name to add the data
* @param array $data An one-dimensional 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
foreach ($data as $name => $value) {
if ($vars != "") $vars .= ", ";
if ($vals != "") $vals .= ", ";
return $this->query("REPLACE INTO `{$table}` ({$vars}) VALUES ({$vals})");
* Performs a query and returns true if the results are not empty
* @param string $query The query to execute
* @return bool Returns false in case of error or empty resultset, or true otherways
$this->errPosition = "performing polling query '<strong>{$query}</strong>'";
//debug_error($this->get_error_message(),ERR_WARNING);
* Performs a query and returns the first value of the first row or false in case of error
* @param string $query The query to execute
* @return bool|string Returns false in case of error or the first row's first field value
//debug_error($this->get_error_message(),ERR_WARNING);
* Update a row on a table. The row to edit is defined by a where clause
* @param string $table The table name from which to edit the data
* @param string $where A MySQL WHERE-formatted query part. This is used to identify the item(s) to edit (ex. "`index` = 2")
* @param array $data An one-dimensional array that contains the field names (as keys) and the field values to edit
* @return bool|resource Returns false in case of error or the resultset of the executed query
function update($table, $index_data, $insert_data) {
foreach ($index_data as $n => $v) {
if ($data!= '') $data.= ' AND ';
foreach ($insert_data as $name => $value) {
if ($q != "") $q .= ", ";
return $this->query("UPDATE `{$table}` SET {$q} WHERE {$where}");
* Remove one or more rows from a table.
* @param string $table The table name from which to edit the data
* @param string $where A MySQL WHERE-formatted query part or an array containing the indexing values
* @return bool|resource Returns false in case of error or the resultset of the executed query
function delete($table, $index_data= false) {
foreach ($index_data as $n => $v) {
if ($data!= '') $data.= ' AND ';
//$data.='`'.$n.'` = \''.mysql_escape_string($v).'\'';
} elseif ($index_data=== false) {
return $this->query("DELETE FROM `{$table}` WHERE {$where}");
* Select one or more rows from a table.
* @param string $table The table name from which to edit the data
* @param string $index_data A MySQL WHERE-formatted query part or an array containing the indexing values
* @param string $return_data A MySQL SELECT-formatted query part or an array containing the values to return
* @return bool|resource Returns false in case of error or the resultset of the executed query
function select($table, $index_data= false, $return_data= false, $order_by= false, $sort_order= false) {
foreach ($return_data as $v) {
if ($data!= '') $data.= ',';
} elseif ($return_data=== false) {
foreach ($index_data as $n => $v) {
if ($data!= '') $data.= ' AND ';
//$data.='`'.$n.'` = \''.mysql_escape_string($v).'\'';
} elseif ($index_data=== false) {
$where= ' WHERE '. $index_data;
if ($order_by !== false) {
if ($sort_order !== false) {
return $this->query("SELECT {$what} FROM `{$table}` {$where}{$order}");
* List the database tables
* @return bool|array Returns false in case of error or the tables as array
$ans= $this->query("show tables");
* Show the database structure
* @param string $table The table name
* @return bool|array Returns false in case of error or the table columns as associative array (Key = column name)
$ans= $this->query("show columns from `". $table. "`");
* Converts a UNIX timestamp into SQL timestamp
* @param int $timestamp A UNIX timestamp value
* @return string Returns the value into MySQL timestamp format
return date("YmdHis", $timestamp);
* Converts a UNIX timestamp into SQL timestamp
* @param string $timestamp A MySQL timestamp value
* @return int Returns the value into UNIX timestamp format
return mktime($h, $i, $s, $m, $d, $y);
* @param string $file The filename to load and run
* @return bool Returns true if all the queries were successfull or false if one query failed
if (substr($row,0,2) == '/*') $incomment = true;
if (substr($row,- 2) == '*/') $incomment = false;
$queries = explode(";\n", $buffer);
foreach ($queries as $query) {
if (!$this->query($query)) return false;
* Visualize the queries being executed for debug purposes
* @return string Returns an HTML formatted result with the queries and their status
$ans = '<table border="1" width="100%">';
$ans .= '<tr><td>'. $query['query']. '</td>';
$ans .= '<td><font color="green">OK</font></td>';
$ans .= '<td>Returned/Affected '. $query['rows']. ' rows</td>';
$ans .= '<td colspan="2">'. $query['error']. '</td>';
|