Source for file MySQLDatabase.class.php
Documentation is available at MySQLDatabase.class.php
if( !extension_loaded( 'mysql' ) )
* For including this file you have to define the constant "CLASSPATH".
* Because every include in the framework depends on the CLASSPATH definition.
* The CLASSPATH means the relative path to the folder that contains the
echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
* Including language specific messages.
include( CLASSPATH. "core/lang_spec_values/". LANG. ".inc.php" );
echo "<h3>You have to define the constant LANG!</h3>\r\n";
echo "Example for german: define( 'LANG', 'de' );\r\n";
* Loading global settings for GilliGan.
require_once( CLASSPATH. "settings.inc.php" );
include_once( CLASSPATH. "core/PlainTextFormatter.class.php" );
* Loading the parent class.
require_once( CLASSPATH. "databases/Database.class.php" );
include_once( CLASSPATH. "databases/mysql/MySQLDatabaseTable.class.php" );
include_once( CLASSPATH. "filesystem/Files.class.php" );
include_once( CLASSPATH. "filesystem/FilesystemToolkit.class.php" );
$GLOBALS["GilliGan_MySQL_QueryLog"] = "";
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (C) 2004 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library 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 Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
&& get_class( $parent ) == "mysqldatabasehost"
&& is_a( $parent, "MySQLDatabaseHost" )
echo "<h3>You have to give a name to this database!</h3>";
echo "<b>Use following Syntax:<b>\r\n\r\n";
echo " \x24db = new MySQLDatabase( \x24name, $parent = \"\" );\r\n\r\n";
echo "<b>BACKTRACE DEBUG:</b>\r\n";
* Sets the references to objects of database-tables to the attribute
* @param string $regExp Makes sure that only database-tables were stored,
* which names matching the given regular expression.
* @param boolean $resFld Cause whether the tables's fields should be
$this->dbt[$i]->setParent( $this );
$this->dbt[$i]->researchFields();
$this->dbt[$t]->setParent( $this );
$this->dbt[$t]->researchFields();
* @param string $regExp Makes sure that only database-tables were stored,
* which names matching the given regular expression.
function dump( $target = "./__dumps", $regExp = "" )
echo "<b>Database</b> ". $this->name. "<br />\r\n";
for( $i = 0; $i < count( $this->dbt ); $i++ )
$target. "/". $this->name. "/"
. $this->dbt[$i]->name. ".sql"
$f->setContent( $this->dbt[$i]->getDump() );
echo $this->dbt[$i]->getLastQuery(). "<br />\r\n";
$target. "/". $this->name. "/"
. $this->dbt[$i]->name. ".sql",
* Imports Dumps from given source to this database.
* @param string $regExp Makes sure that only files will be imported,
* which names matching the given regular expression.
function importDump( $source = "./__dumps", $regExp = "" )
for( $i = 0; $i < count( $tmp_arr ); $i++ )
$tmp_data_order = "-e \"source ". $source. "/". $tmp_arr[$i]["name"]. "\" "
. "/mysql ". $tmp_data_order;
for( $i = 0; $i < count( $tmp_arr ); $i++ )
$tmp_f = new Files( $source. "/". $tmp_arr[$i]["name"] );
$str_tmp = &$tmp_f->getContent();
* Researches a whole folder to import dumped data from it.
* @param string $source The source resource of the MySQL-Dump.
if ( $f = fopen( $source, 'r' ) )
$data = $str_rest. fgets( $f );
"!;\s+(?:INSERT|REPLACE|UPDATE|DROP|CREATE) !is",
$length = $pos_new - $pos;
// Check whether the last tok ends with a html entity or
&& preg_match( "!&(?:[a-z0-9]{2,9}|#\d{1,5})$!i", $query )
if ( !preg_match( "!&(?:[a-z0-9]{2,9}|#\d{1,5})$!i", $query ) )
$str_rest = substr( $data ,$pos );
$str_rest = trim( $str_rest );
if ( !empty( $str_rest ) )
$this->query( $str_rest );
// * - %databasetable% -> The name of the database in which an error occurs.
* This function parses pseudo variables in a text on SQL-errors.
* - %database% -> The name of this database.
* - %query% -> The query which was tried to execute.
* @param string $database
// $out = preg_replace( "!\%databasetable\%!i", $databasetable, $out );
* Will query this database.
* @param string $sqlOrder
* @param boolean $no_logs
function query( $sqlOrder, $no_logs = false )
static $int_query_cnt = 1;
{ $query_microtime_begin = time() + microtime(); }
if ( $this->probeConnection() )
$result = mysql_db_query(
$query_microtime = time() + microtime() - $query_microtime_begin;
$GLOBALS["GilliGan_MySQL_QueryLog"] .= "\r\n<!--\r\nQuery "
"--[ADDED TO AVOID CLOSING OF EMBRACING COMMENT]>",
. "\r\n------------------------------------------\r\n"
. "\r\n------------------------------------------\r\n"
$GLOBALS["GilliGan_MySQL_QueryExecutionTime"] += $query_microtime;
// Access denied for user
$tmp_name( $errno, $err );
$tmp_name( $errno, $err );
. "FROM __admin_mysql_errorlogs "
. "WHERE script_name = '". $_SERVER["SCRIPT_NAME"]. "' "
. "AND mysql_errno = '". $errno. "' "
"UPDATE __admin_mysql_errorlogs "
. "SET mysql_error = '". str_replace( "'","\'", $err ). "', "
. "mysql_errno = '". $errno. "', "
. "mysql_query = '". str_replace( "'","\'", $sqlOrder ). "', "
. "WHERE script_name = '". $_SERVER["SCRIPT_NAME"]. "'", true
"INSERT INTO __admin_mysql_errorlogs "
. "SET mysql_error = '". str_replace( "'","\'", $err ). "', "
. "mysql_errno = '". $errno. "', "
. "mysql_query = '". str_replace( "'","\'", $sqlOrder ). "', "
. "script_name = '". $_SERVER["SCRIPT_NAME"]. "'", true
$GLOBALS["email_subject"]["sqlerror"],
$GLOBALS["email_body"]["sqlerror"]
. "Reply-To: \"noreply\" <>\r\n"
$res_arr = mysql_fetch_assoc( $res )
if( empty( $res_arr["active"] ) )
echo "<b>An error occurs!</b>\r\n";
echo "<b>BACKTRACE DEBUG:</b>\r\n";
echo "<b>ISO-Occurrance-Date:</b> "
. date( "Y-m-d H:i:s" ). "<br /><br />\r\n";
echo "<b>MySQL says:</b><br />\r\n";
echo $err. "<br /><br />\r\n";
echo "<b>Your query is:</b><br />\r\n";
* Imports a given SQL-string into this Database.
* @param string $str_dump The dump to import.
$tmp_reg_exp = "(\s*+|--[^\r\n]*+)*{$tmp_look_ahead}";
$str_dump = preg_replace( "!^{$tmp_reg_exp}!is", "", $str_dump );
$order_arr = preg_split( "!;{$tmp_reg_exp}!is", $str_dump );
foreach ( $order_arr as $str_query )
$this->query( $str_query );
* Probes whether there is a connection to the databasehost, if not it will
* attempt to connect to the host.
echo "There is no valid parent-object for the database named "
. "\"". $this->name. "\".<br />\r\n"
. "Please add this database-object to a databasehost-object before "
. "you make a call such as on <b>line "
. $debugArr[count($debugArr)- 1]["line"]. "</b> in <b>file \""
. $debugArr[count($debugArr)- 1]["file"]. "\"</b>."
echo "<b>BACKTRACE DEBUG:</b><br />\r\n";
if( !$this->parent->is_connected )
if( $this->parent->is_connected )
} // END of class MySQLDatabase
|