mysql.lib.php
上传用户:jiangbw
上传日期:2022-03-16
资源大小:49k
文件大小:38k
- <?
- /*======================================================================*
- || #################################################################### ||
- || # BUILD UNDER PHP SCRIPTNET 3.2.1 FRAMEWORK ||
- || # ---------------------------------------------------------------- # ||
- || # Code2Art Open Source Software. All Rights Reserved. ||
- || # This file should be redistributed in whole or significant part. # ||
- || # ------------------ SCRIPTNET IS FREE SOFTWARE ------------------ # ||
- || # http://www.code2art.com | http://www.code2art.com/scriptnet # ||
- || # Copyleft by Benediktus Ardian Hersanto, SE # ||
- || # Sorry if this framework is still unavailable for public # ||
- || # because I still searching on my own head. # ||
- || #################################################################### ||
- *======================================================================*/
- /*======================================================================*
- || #################################################################### ||
- || DBF to MySQL Bulk Converter 2.0 ||
- ||______________________________________________________________________||
- || This utility was developed on 2006, since this were used to be on ||
- || my own library. But I think I should share it to you, because I ||
- || need to save my work on Internet (I don't believe on my own HD drive)||
- || it always crash and had a terrible bad sectors. ||
- || Thanks to all person who download this utility, I hope you enjoy it. ||
- || ||
- || Regards, ||
- || Benediktus Ardian Hersanto,SE (ardie_b@yahoo.com) ||
- /*======================================================================*/
- // ---------------------------------------------------------------------//
- // Free to use for everyone who understand PHP
- // ---------------------------------------------------------------------//
- // Last Update : 27.03.2007
- // CHANGES :
- // MySQL Exception Handler :
- // Added : Assert command on handling error of mysql execution
- // --------------------------------------------------------------------------------
- // Free to use to everyone who understand PHP
- // --------------------------------------------------------------------------------
- /*
- *@author B. Ardian Hersanto
- *
- *@copyright GNU as long as you're friends of mine, just kidding
- *@package DBComponents
- */
- $basename = explode('.',basename(__FILE__));
- $file_ext = array_pop($basename);
- $lib_dir = str_replace('\','/',dirname(__FILE__));
- $filename = $lib_dir.'/rdbms.cls.' . FILE_EXT;
- define('DB_ROOT_CLASS',$filename);
- if(file_exists(DB_ROOT_CLASS)) include(DB_ROOT_CLASS);
- if(!isset($_LIBRARY[basename(__FILE__)]))
- $_LIBRARY[basename(__FILE__)] = "mysql";
- // This MySQLException are trapping incorrect SQL syntax, the main core error are handled by debugger.lib.php
- function MySQLException($file, $line, $code)
- {
- global $_ERROR;
- $_ERROR = new stdclass;
- $_ERROR->file = $file;
- $_ERROR->line = $line;
- $_ERROR->code = $code;
- if(ini_get('display_errors')==1) die('<b>MySQL Syntax ERROR :</b> '.mysql_error());
- trigger_error(mysql_error(),E_USER_ERROR);
- error_log("[".date("d-m-Y")." ".date("H:i:s")."] [client ".$_SERVER['REMOTE_ADDR']."] [FILE:".$file.", LINE:".$line."] [".mysql_error()."]rn", 3, ROOT_DIR.DB_ERROR_LOG);
- }
- class MySQL extends RDBMS {
- // These variables are used in START TRANSACTION MODE
- // NOTES : THE CHANGES TO TABLE IS AFFECTED ONLY TO InnoDB Type Table
- // THESE FLAG WILL IGNORED IF YOUR TABLE TYPE IS MyISAM TABLE
- var $safeMode = true;
- var $COMMIT_CHANGES = false;
- var $unbuffered = false;
-
- //These variables will evaluate itself on each query event
- var $onUpdate, $onBrowse, $onClose, $onDelete, $onInsert, $onQueryStart;
-
- //Library Properties
- var $about = "GNU PHP - MySQL Component for PHP 4";
- //var $error_log = true;
- //var $log_file = "AppError.txt"; //Already defined on RDBMS Class
- var $version = "4.2.06-dev.27.01";
-
- function MySQL()
- {
- if(!function_exists('mysql_connect'))
- trigger_error('<p/><b/><font color="#FF0000" face="Verdana" size="2" onmouseover=""/>MySQL DB Component dependency problem<table border="0" width="29%" cellspacing="1" style="border:1px solid #000080;background-color:#D5E6DC"/><tr/><td/><font face="Verdana" size="2" color="#FF0000"/>php_mysql.dll is needed to run this component or your current PHP version is no longer support the original mysql function from PHP 4', E_ERROR);
-
- // Inherit create from base component RDBMS
- parent::_create();
-
- // Error Handler
- assert_options(ASSERT_ACTIVE, 1);
- assert_options(ASSERT_WARNING, 0);
- assert_options(ASSERT_QUIET_EVAL, 1);
- assert_options(ASSERT_CALLBACK, 'MySQLException');
-
- // Initial Connection
- if($this->Connect()) {
- if(!defined("__MYSQL")) define('__MYSQL',1);
- } else {
- trigger_error($this->TrackError(), $this->error_priority[0]);
- }
- $this->mysql_version = $this->GetServerVersion();
- }
-
- function _create() { parent::_create(); }
-
- function _destroy() { parent::_destroy(); }
- function TrackError()
- {
- $this->error_msg = mysql_error();
- $this->inherited(__FUNCTION__);
- }
-
- function TestConnection($dbname,$dbhost,$dbport,$dbuser,$dbpassword)
- {
- $cid = @mysql_connect($dbhost.":".$dbport,$dbuser,$dbpassword);
- if ($cid) {
- if(@mysql_select_db ($dbname,$cid)) {
- @mysql_close($cid);
- return true;
- } else {
- @mysql_close($cid);
- return false;
- }
- } else {
- @mysql_close($cid);
- return false;
- }
- }
- function Connect()
- {
- if($this->port!='3306') $this->hostname .= ':'.$this->port;
- $this->db = @mysql_connect($this->hostname,$this->username,$this->password);
- if($this->db) {
- if(@mysql_select_db($this->database)) {
- /* Too much consume memory if there is more than 10000 tables
- if(is_array($this->Tables)) $this->Tables = array();
- $this->Tables = array_merge($this->Tables,$this->ListTables());*/
- $this->connected = true;
- return true;
- } else return false;
- } else {
- //trigger_error($this->TrackError(), E_USER_ERROR); // UnComment this line if you need to connect manually without using default constructor connection
- return false;
- }
- }
-
- function Reconnect($dbname,$hostname='',$username='',$password='')
- {
- if(!empty($hostname)) $this->hostname=$hostname;
- if(!empty($username)) $this->username=$username;
- if(!empty($password)) $this->password=$password;
- $this->db = @mysql_connect($this->hostname,$this->username,$this->password);
- if($this->db) {
- if(@mysql_select_db($dbname)) {
- $this->connected = true;
- $this->database = $dbname;
- /*if(is_array($this->Tables)) {
- $this->Tables = array();
- $this->Tables = array_merge($this->Tables,$this->ListTables());
- }*/
- return true;
- } else return false;
- } else {
- //trigger_error($this->TrackError(), E_USER_ERROR); // UnComment this line if you need to connect manually without using default constructor connection
- return false;
- }
- }
-
- function Browse($fields,$table,$exp='',$page='',$limit='',$urlquery='',$encode_method="base64_encode",$func_type="php")
- {
- return $this->Select($fields,$table,$exp,$page,$limit,$urlquery,$encode_method,$func_type);
- }
-
- function Select($fields,$table,$exp='',$page='',$limit='',$urlquery='',$encode_method="",$func_type="php")
- {
- if(strlen(trim($page))>0) if(!settype($page,"integer")) return;
- if(strlen(trim($limit))>0) if(!settype($limit,"integer")) return;
- if((is_int($page))&&(is_int($limit))) {
- parent::createNavigator($table,$exp,$page,$limit,$urlquery,$encode_method,$func_type);
- $paging = $this->SQL_LIMIT;
- } else $paging = '';
- if (empty($exp)) $this->command = "select {$fields} from {$table}".$paging;
- else $this->command = "select {$fields} from {$table} where {$exp}".$paging;
- if($this->unbuffered) $status = assert('$this->pid = @mysql_unbuffered_query($this->command)');
- else $status = assert('$this->pid = @mysql_query($this->command)');
- $results = array();
- if($status) {
- $this->active_table = $table;
- if ($this->pid) {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1">Executing SELECT STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- if(!$this->unbuffered) {
- $this->RecordCount = @mysql_num_rows($this->pid);
- if ($this->RecordCount>0) {
- if($this->return_as_array) {
- while ($data=@mysql_fetch_array($this->pid)) $results[] = $data;
- mysql_free_result($this->pid);
- if(!empty($this->onBrowse)) $this->onBrowse();
- return $results;
- } else {
- while ($data=@mysql_fetch_object($this->pid)) $results[] = $data;
- mysql_free_result($this->pid);
- if(!empty($this->onBrowse)) $this->onBrowse();
- return $results;
- }
- } else return NULL;
- } else {
- while ($data=@mysql_fetch_array($this->pid)) {
- $results[] = $data;
- if(count($results)>5000) die('Data is too large to read please provide paging parameter');
- }
- mysql_free_result($this->pid);
- return $results;
- }
- } else {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing SELECT STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->TrackError();
- return false;
- }
- }
- }
-
- function GetServerVersion()
- {
- $data = @mysql_query('select version()');
- $data = mysql_fetch_array($data);
- return $data[0];
- }
- function Update($table,$value,$exp='')
- {
- if($this->safeMode) {
- if(preg_match("/^4.*/i",$this->mysql_version)) {
- $uid = mysql_query('SET AUTOCOMMIT=0');
- $uid = mysql_query('START TRANSACTION');
- } else if (preg_match("/^3.*/i",$this->mysql_version)) {
- $uid = mysql_query('SET AUTOCOMMIT=0');
- $uid = mysql_query('BEGIN WORK');
- }
- } else {
- $uid = mysql_query('SET AUTOCOMMIT=1');
- }
- if (empty($exp)) $this->command = "update {$table} set {$value}";
- else $this->command = "update {$table} set {$value} where {$exp}";
- if(assert('$this->uid = mysql_query($this->command)')) {
- if ($this->uid) {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing UPDATE STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->AffectedRows = mysql_affected_rows();
- if(!empty($this->onUpdate)) $this->onUpdate();
- if($this->safeMode) {
- if($this->COMMIT_CHANGES) {
- $uid = mysql_query('COMMIT');
- } else {
- $uid = mysql_query('ROLLBACK');
- }
- }
- return true;
- } else {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing UPDATE STATEMENT on file : <b><font color=#FF0000>",basename($this->FILE),"</font></b> line <b><font color=#0000FF>",$this->LINE,"</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>",$this->command,"</i>"</font></font><br><hr>";
- $this->TrackError();
- return false;
- }
- }
- }
- function Delete($table,$exp='')
- {
- if($this->safeMode) {
- if(preg_match("/^4.*/i",$this->mysql_version)) {
- $uid = mysql_query('SET AUTOCOMMIT=0');
- $uid = mysql_query('START TRANSACTION');
- } else if (preg_match("/^3.*/i",$this->mysql_version)) {
- $uid = mysql_query('SET AUTOCOMMIT=0');
- $uid = mysql_query('BEGIN WORK');
- }
- } else {
- $uid = mysql_query('SET AUTOCOMMIT=1');
- }
- if($this->simulate) { $this->debug=true; $tmp = $this->Browse("*",$table,$exp); }
- if (empty($exp)) $this->command = "delete from {$table}";
- else $this->command = "delete from {$table} where {$exp}";
- if(assert('$this->eid = mysql_query($this->command)')) {
- if ($this->eid) {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing DELETE STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->AffectedRows = mysql_affected_rows();
- if(!empty($this->onDelete)) $this->onDelete();
- if($this->safeMode) {
- if($this->COMMIT_CHANGES) {
- $uid = mysql_query('COMMIT');
- } else {
- $uid = mysql_query('ROLLBACK');
- }
- }
- if($this->simulate) {
- for($i=0;$i<count($tmp);$i++) {
- $this->Insert($table,implode(",",$this->getFieldsFromObject($tmp)),implode(",",$this->getDataFromObject($tmp,"'")));
- }
- $this->debug = false;
- }
- return true;
- } else {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing DELETE STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->TrackError();
- return false;
- }
- }
- }
-
- //function Replace($table,$values
-
- function InsertSelect($dest_fields,$dest_table,$src_fields,$src_table,$exp='',$ignore=true) {
- if($this->safeMode) {
- if(preg_match("/^4.*/i",$this->mysql_version)) {
- $uid = mysql_query('SET AUTOCOMMIT=0');
- $uid = mysql_query('START TRANSACTION');
- } else if (preg_match("/^3.*/i",$this->mysql_version)) {
- $uid = mysql_query('SET AUTOCOMMIT=0');
- $uid = mysql_query('BEGIN WORK');
- }
- } else {
- $uid = mysql_query('SET AUTOCOMMIT=1');
- }
- if (empty($exp))
- $this->command = "Insert ".(($ignore)?"IGNORE":"")." into {$dest_table}({$dest_fields}) SELECT {$src_fields} FROM {$src_table}";
- else
- $this->command = "Insert ".(($ignore)?"IGNORE":"")." into {$dest_table}({$dest_fields}) SELECT {$src_fields} FROM {$src_table} WHERE {$exp}";
- $this->pid = @mysql_query($this->command);
- if ($this->pid) {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing INSERT STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- if($this->safeMode) {
- if($this->COMMIT_CHANGES) {
- $uid = mysql_query('COMMIT');
- } else {
- $uid = mysql_query('ROLLBACK');
- }
- }
- return true;
- } else {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing INSERT STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->TrackError();
- return false;
- }
-
- }
- function Insert($table,$fields,$values,$ignore=false)
- {
- if($this->safeMode) {
- if(preg_match("/^4.*/i",$this->mysql_version)) {
- $uid = mysql_query('SET AUTOCOMMIT=0');
- $uid = mysql_query('START TRANSACTION');
- } else if (preg_match("/^3.*/i",$this->mysql_version)) {
- $uid = mysql_query('SET AUTOCOMMIT=0');
- $uid = mysql_query('BEGIN WORK');
- }
- } else {
- $uid = mysql_query('SET AUTOCOMMIT=1');
- }
- $this->command = "Insert ".(($ignore)?"Ignore":"")." into {$table}({$fields}) values ({$values})";
- if(assert('$this->pid = mysql_query($this->command)')) {
- $this->last_record_id = mysql_insert_id($this->db);
- if ($this->pid) {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing INSERT STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- if(!empty($this->onInsert)) $this->onInsert();
- if($this->safeMode) {
- if($this->COMMIT_CHANGES) {
- $uid = mysql_query('COMMIT');
- } else {
- $uid = mysql_query('ROLLBACK');
- }
- }
- return true;
- } else {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing INSERT STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->TrackError();
- return false;
- }
- } else {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing INSERT STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->TrackError();
- return false;
- }
- }
-
- function Replace($table,$fields,$values)
- {
- if($this->safeMode) {
- if(preg_match("/^4.*/i",$this->mysql_version)) {
- $uid = mysql_query('SET AUTOCOMMIT=0');
- $uid = mysql_query('START TRANSACTION');
- } else if (preg_match("/^3.*/i",$this->mysql_version)) {
- $uid = mysql_query('SET AUTOCOMMIT=0');
- $uid = mysql_query('BEGIN WORK');
- }
- } else {
- $uid = mysql_query('SET AUTOCOMMIT=1');
- }
- $this->command = "Replace into {$table}({$fields}) values ({$values})";
- if(assert('$this->pid = mysql_query($this->command)')) {
- if ($this->pid) {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing REPLACE STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- if(!empty($this->onInsert)) $this->onInsert();
- if($this->safeMode) {
- if($this->COMMIT_CHANGES) {
- $uid = mysql_query('COMMIT');
- } else {
- $uid = mysql_query('ROLLBACK');
- }
- }
- return true;
- } else {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing REPLACE STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->TrackError();
- return false;
- }
- }
- }
- function Query($command,$return_result=0)
- {
- settype($command,"string");
- $this->command = (string)str_replace("rn","",stripslashes($command));
- if(assert('$this->pid = mysql_query($this->command,$this->db)')) {
- if ($this->pid) {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1">Executing QUERY STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- if(!empty($this->onQueryStart)) $this->onQueryStart();
- if($return_result) {
- $this->AffectedRows = @mysql_affected_rows();
- $this->RecordCount = @mysql_num_rows($this->pid);
- if($this->return_as_array) {
- $time0 = time();
- while ($data=@mysql_fetch_array($this->pid)) {
- $results[] = $data;
- $time1 = time();
- if ($time1 >= $time0 + 30) {
- $time0 = $time1;
- header('X-pmaPing: Pong');
- }
- }
- mysql_free_result($this->pid);
- } else {
- $time0 = time();
- while ($data=@mysql_fetch_object($this->pid)) {
- $results[] = $data;
- $time1 = time();
- if ($time1 >= $time0 + 30) {
- $time0 = $time1;
- header('X-pmaPing: Pong');
- }
- }
- mysql_free_result($this->pid);
- }
- if(isset($results)) return $results; else return NULL;
- } else
- return $this->pid;
- } else {
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1" color="#ff0000">Executing QUERY STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->TrackError();
- return false;
- }
- }
- }
- function getRecordData($table,$expression='',$buffer_name='')
- {
- $rec = $this->Browse("*",$table,$expression);
- if(count($rec)>0) {
- if(!empty($buffer_name)) {
- global $$buffer_name;
- $$buffer_name = $rec[0];
- } else {
- global $RECORD;
- $RECORD = $rec[0];
- }
- }
- }
-
- function getFieldDataType($database,$table,$fieldname) {
- $result = mysql_query("SHOW COLUMNS FROM `{$database}`.`{$table}`");
- if (!$result) {
- echo 'Could not run query: ' . mysql_error();
- exit;
- }
- if (mysql_num_rows($result) > 0) {
- while ($row = mysql_fetch_assoc($result)) {
- if($row['Field']==$fieldname) {
- return $row['Type'];
- break;
- }
- }
- }
- }
-
- function getCustomRecordData($fields,$table,$expression='',$buffer_name='')
- {
- $rec = $this->Browse($fields,$table,$expression);
- if(count($rec)>0) {
- if(!empty($buffer_name)) {
- global $$buffer_name;
- $$buffer_name = $rec[0];
- } else {
- global $RECORD;
- $RECORD = $rec[0];
- }
- }
- }
-
- function getFieldData($field_name,$fields,$table,$exp='')
- {
- $this->return_as_array = true;
- $rec = $this->Select($fields,$table,$exp);
- $this->return_as_array = false;
- if(count($rec)>0) {
- return $rec[0][$field_name];
- } else return;
- }
- function Count($items,$table,$exp='')
- {
- if (empty($exp)) $this->command = "select count({$items}) from {$table}";
- else $this->command = "select count($items) from {$table} where {$exp}";
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1">Executing COUNT STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->pid = @mysql_query($this->command);
- if($this->pid) {
- if(mysql_num_rows($this->pid)>1) return mysql_num_rows($this->pid); else {
- $total = @mysql_fetch_array($this->pid);
- return $total[0];
- }
- } else {
- $this->TrackError();
- }
- }
- function Sum($field,$table,$exp='')
- {
- if (empty($exp)) $this->command = "select sum({$field}) from {$table}";
- else $this->command = "select sum({$field}) from {$table} where {$exp}";
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1">Executing SUM STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->pid = @mysql_query($this->command);
- if($this->pid) {
- $total = @mysql_fetch_array($this->pid);
- return (empty($total[0]))?0:$total[0];
- } else {
- $this->TrackError();
- }
- }
-
- function Max($field,$table,$exp='')
- {
- if (empty($exp)) $this->command = "select max({$field}) from {$table}";
- else $this->command = "select max({$field}) from {$table} where {$exp}";
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1">Executing MAX STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->pid = @mysql_query($this->command);
- if($this->pid) {
- $total = @mysql_fetch_array($this->pid);
- return (empty($total[0]))?0:$total[0];
- } else {
- $this->TrackError();
- }
- }
-
- function Min($field,$table,$exp='')
- {
- if (empty($exp)) $this->command = "select min({$field}) from {$table}";
- else $this->command = "select min({$field}) from {$table} where {$exp}";
- if ($this->debug) echo "<hr><br><font face="Verdana" size="1">Executing MIN STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- $this->pid = @mysql_query($this->command);
- if($this->pid) {
- $total = @mysql_fetch_array($this->pid);
- return (empty($total[0]))?0:$total[0];
- } else {
- $this->TrackError();
- }
- }
- function getRecordLen($table) {
- return $this->Count("*",$table);
- }
-
- function TruncateTable($table) {
- $pid = mysql_query("TRUNCATE TABLE ".$table);
- if($pid) return true; else die(mysql_error());
- }
- function DumpTableToTemp($data,$file,$separator)
- {
- $fp = fopen($file,'w');
- if($fp) {
- fwrite($fp,ereg_replace(",",$separator,$this->GetFieldNames($data,$this->active_table))."rn");
- for($i=0;$i<count($data);$i++) {
- $fields = get_object_vars($data[$i]);
- fwrite($fp,implode($separator,array_values($fields))."rn");
- }
- }
- fclose($fp);
- }
- function DumpTableToExcel($tablename)
- {
- header("Content-type: application/vnd.excel");
- $fields = @mysql_list_fields($this->dbname, $tablename, $this->db);
- $columns = @mysql_num_fields($fields);
- for ($i = 0; $i < $columns; $i++) {
- echo mysql_field_name($fields, $i)."t";
- }
- echo "rn";
- $records = $this->Query("select * from {$tablename}");
- while ($records = @mysql_fetch_array($this->pid)) {
- for($n=0;$n<$columns;$n++) echo $records[$n]."t";
- echo "rn";
- }
- header("Content-Disposition: attachment; filename={$tablename}.xls");
- }
-
- function getFieldsFromObject($obj)
- {
- if(is_array($obj)) {
- if(count($obj)>0) {
- return array_keys(get_object_vars($obj[0]));
- }
- } else if(is_object($obj)) {
- return array_keys(get_object_vars($obj));
- }
- }
-
- function getFieldsFromAssoc($obj,$type='integer')
- {
- if(is_array($obj)) {
- if(count($obj)>0) {
- if(is_array($obj[0]))
- $fields = array_keys($obj[0]); else
- $fields = array_keys($obj);
- for($n=0;$n<count($fields);$n++) {
- if($type=='integer') {
- if(is_int($fields[$n])) $field[] = $fields[$n];
- } else if($type=='string') {
- if(is_string($fields[$n])) $field[] = $fields[$n];
- }
- }
- return $field;
- }
- } else if(is_object($obj)) {
- return array();
- }
- }
-
- function getDataFromObject($obj,$quote='')
- {
- if(is_array($obj)) {
- if(count($obj)>0) {
- if(empty($quote)) {
- return array_values(get_object_vars($obj[0]));
- } else {
- $dat = array_values(get_object_vars($obj[0]));
- for($i=0;$i<count($dat);$i++) $dat[$i] = $quote.$dat[$i].$quote;
- return $dat;
- }
- }
- } else if(is_object($obj)) {
- if(empty($quote)) {
- return array_values(get_object_vars($obj));
- } else {
- $dat = array_values(get_object_vars($obj));
- for($i=0;$i<count($dat);$i++) $dat[$i] = $quote.$dat[$i].$quote;
- return $dat;
- }
- }
- }
- function ListDatabases()
- {
- $db_list = @mysql_list_dbs($this->db);
- while ($row = @mysql_fetch_array($db_list)) {
- $dbs[] = $row[0];
- }
- if(isset($dbs)) return $dbs; else return NULL;
- }
- function CreateDatabase($dbname,$auth_user,$auth_password)
- {
- $this->Close(); $current_user = $this->username; $this->username = $this->admin;
- $current_password = $this->password; $this->password = $this->dba_password; $current_database = $this->database; $this->database = "mysql";
- if ($this->Connect()) {
- $this->Query("Create database {$dbname}");
- $this->Query("grant all privileges on {$dbname}.* to {$auth_user}@{$this->hostname} identified by '{$auth_password}'");
- $this->Close();
- $this->username = $current_user;
- $this->password = $current_password;
- $this->database = $current_database;
- $this->Connect();
- return true;
- } else {
- $this->TrackError();
- return false;
- }
- }
-
- function getFieldName($sql,$fieldname) {
- $this->return_as_array=1;
- $res = $this->Query($sql);
- if($res) {
- $data = mysql_fetch_array($res);
- $this->return_as_array=0;
- return $data[$fieldname];
- } else {
- $this->TrackError();
- return false;
- }
- }
- function GetFieldNames($data,$table='')
- {
- if(is_array($data)&&(count($data)>0)) {
- if(isset($data[0])) {
- if(is_object($data[0])) {
- $fields = get_object_vars($data[0]);
- return implode(",",array_keys($fields));
- } else if(is_array($data[0])) {
- return implode(",",array_keys($data[0]));
- }
- }
- } else {
- $fields = @mysql_list_fields($this->database, $table, $this->db);
- $columns = @mysql_num_fields($fields);
- for($i=0;$i<$columns;$i++) $data[] = @mysql_field_name($fields, $i);
- return implode(",",$data);
- }
- }
- function TimeStampExtract($date,$words,$as_array=true)
- {
- if(!$as_array) $date = new stdclass;
- if ($words) {
- if(!$as_array) $date->year = substr($date,0,4); else $year = substr($date,0,4);
- if (substr($date,4,2)=="01") {if(!$as_array) $date->month="January"; else $month = "January"; }
- if (substr($date,4,2)=="02") {if(!$as_array) $date->month="February"; else $month = "February";}
- if (substr($date,4,2)=="03") {if(!$as_array) $date->month="March"; else $month = "March";}
- if (substr($date,4,2)=="04") {if(!$as_array) $date->month="April"; else $month = "April";}
- if (substr($date,4,2)=="05") {if(!$as_array) $date->month="May"; else $month = "May";}
- if (substr($date,4,2)=="06") {if(!$as_array) $date->month="June"; else $month = "June";}
- if (substr($date,4,2)=="07") {if(!$as_array) $date->month="July"; else $month = "July";}
- if (substr($date,4,2)=="08") {if(!$as_array) $date->month="August"; else $month = "August";}
- if (substr($date,4,2)=="09") {if(!$as_array) $date->month="September"; else $month = "September";}
- if (substr($date,4,2)=="10") {if(!$as_array) $date->month="October"; else $month = "October";}
- if (substr($date,4,2)=="11") {if(!$as_array) $date->month="November"; else $month = "November";}
- if (substr($date,4,2)=="12") {if(!$as_array) $date->month="December"; else $month = "December";}
- if(!$as_array) $date->day = substr($date,6,2); else $day = substr($date,6,2);
- if(!$as_array) $date->hour = substr($date,8,2); else $hour = substr($date,8,2);
- if(!$as_array) $date->minutes = substr($date,10,2); else $minutes = substr($date,10,2);
- if(!$as_array) $date->second = substr($date,12,2); else $second = substr($date,12,2);
- if($as_array) {
- return array("year"=>$year,"month"=>$month,"day"=>$day,"hour"=>$hour,"minutes"=>$minutes,"second"=>$second);
- } else {
- return $date;
- }
- } else {
- if(!$as_array) $date->year = substr($date,0,4); else $year = substr($date,0,4);
- if(!$as_array) $date->month = substr($date,4,2); else $month = substr($date,4,2);
- if(!$as_array) $date->day = substr($date,6,2); else $day = substr($date,6,2);
- if(!$as_array) $date->hour = substr($date,8,2); else $hour = substr($date,8,2);
- if(!$as_array) $date->minutes = substr($date,10,2); else $minutes = substr($date,10,2);
- if(!$as_array) $date->second = substr($date,12,2); else $second = substr($date,12,2);
- if($as_array) {
- return array("year"=>$year,"month"=>$month,"day"=>$day,"hour"=>$hour,"minutes"=>$minutes,"second"=>$second);
- } else {
- return $date;
- }
- }
- }
- // Uncomplete Function
- function ReadTableStruct($table_name)
- {
- $result = @mysql_query("SELECT * FROM {$table_name}");
- $fields = @mysql_num_fields($result);
- $table = @mysql_field_table($result, 0);
- for ($i=0; $i < $fields; $i++) {
- $type = mysql_field_type($result, $i);
- $name = mysql_field_name($result, $i);
- $len = mysql_field_len($result, $i);
- $flags = mysql_field_flags($result, $i);
- echo $name . " " .$type." ". $len . " " . $flags . "<br>";
- }
- mysql_free_result($result);
- }
-
- function ListTables($pattern='')
- {
- if($pattern=='') {
- $result = @mysql_list_tables($this->database);
- if (!$result) {
- echo "DB Error, could not list tablesn";
- echo 'MySQL Error: ' . mysql_error();
- exit;
- }
- while ($row = @mysql_fetch_row($result)) {
- $tables[] = $row[0];
- }
- @mysql_free_result($result);
- if(isset($tables)) return $tables; else return NULL;
- } else {
- $ret = $this->Query("SHOW TABLES LIKE '{$pattern}'",1);
- if(is_array($ret)&&count($ret)>0) {
- for($i=0;$i<count($ret);$i++) {
- list($k,$name)=each($ret[$i]);
- $tables[] = $name;
- }
- return $tables;
- } else return NULL;
- }
- }
-
- function TableExists($tableName) {
- $this->command = "Select * From {$tableName}";
- if($this->debug) echo "<hr><br><font face="Verdana" size="1">Executing QUERY STATEMENT on file : <b><font color=#FF0000>".basename($this->FILE)."</font></b> line <b><font color=#0000FF>".$this->LINE."</font></b><br>with SQL COMMAND : <font color=#FF0000>"<i>".$this->command."</i>"</font></font><br><hr>";
- if(@mysql_query($this->command))
- return true;
- else return false;
- }
-
- function ListFields($table_name)
- {
- $fields = mysql_list_fields($this->database, $table_name, $this->db);
- $columns = mysql_num_fields($fields);
- for ($i = 0; $i < $columns; $i++) {
- $field[] = mysql_field_name($fields, $i);
- }
- if(isset($field)) return $field; else return NULL;
- }
-
- function ListFieldNames($tablename,$style='ucfirst') {
- $result = mysql_query("SHOW COLUMNS FROM ".$tablename);
- if (!$result) {
- echo 'Could not run query: ' . mysql_error();
- exit;
- }
- if (mysql_num_rows($result) > 0) {
- while ($row = mysql_fetch_assoc($result)) {
- $fields[] = $style(strtolower($row['Field']));
- }
- }
- if(isset($fields)) return $fields; else return NULL;
- }
-
- function Close() {
- @mysql_close($this->db);
- if(!empty($this->onClose)) eval($this->onClose);
- $this->connected = false;
- }
- /*function getFieldName($sql,$fName)
- {
- $rec = @mysql_query($sql);
- $ret=mysql_fetch_array($rec);
- mysql_free_result($rec);
- return $ret[$fName];
- }*/
-
- function ExportTable($table_name,$fileoutput,$savefile=false,$compress=false) {
- if(isset($this->db)) {
- if($table_name=="*") {
- $tables = $this->ListTables();
- set_time_limit(0);
- for($i=0;$i<count($tables);$i++) {
- $pid = mysql_query("SHOW CREATE TABLE ".$tables[$i]);
- if($pid) {
- $struct = mysql_fetch_array($pid);
- $this->results = "DROP TABLE IF EXISTS ".$tables[$i].";rnrn";
- $this->results .= $struct[1];
- $contents = mysql_query("SELECT * FROM ".$tables[$i]);
- if($contents) {
- while($data = mysql_fetch_array($contents)) {
- $this->results .= "rnrnINSERT INTO ".$tables[$i]." VALUES (";
- if(isset($datas)) unset($datas);
- while(list($k,$v)=each($data)) {
- if(is_string($k))
- $datas[] = "'".$data[$k]."'";
- }
- if(isset($datas)&&is_array($datas))
- $this->results .= implode(",",$datas);
- $this->results .= ");";
- }
- if(function_exists('gzcompress')) {
- $compressed_data[] = gzcompress($this->results,9);
- }
- } else {
- trigger_error('Error while dumping table data due the following error : '.mysql_error(),E_USER_ERROR);
- }
- unset($this->results);
- } else {
- trigger_error('Cannot Execute SHOW CREATE TABLE due the following error : '.mysql_error(),E_USER_ERROR);
- }
- }
- $this->results = gzcompress(implode("(:EOF:)",$compressed_data));
- if($savefile) {
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header('Pragma: public');
- //header('Transfer-Encoding: chunked');
- header('Content-Type: application/octet-stream');
- header('Content-Length: '.strlen($this->results));
- header('Content-Disposition: attachment; filename="'.$fileoutput.'"');
- echo $this->results;
- }
- return TRUE;
- } else {
- $pid = mysql_query("SHOW CREATE TABLE ".$table_name);
- if($pid) {
- $struct = mysql_fetch_array($pid);
- $this->results = "DROP TABLE IF EXISTS ".$table_name.";rnrn";
- $this->results .= $struct[1];
- $contents = mysql_query("SELECT * FROM ".$table_name);
- if($contents) {
- while($data = mysql_fetch_array($contents)) {
- $this->results .= "rnrnINSERT INTO ".$table_name." VALUES (";
- if(isset($datas)) unset($datas);
- while(list($k,$v)=each($data)) {
- if(is_string($k))
- $datas[] = "'".$data[$k]."'";
- }
- if(isset($datas)&&is_array($datas))
- $this->results .= implode(",",$datas);
- $this->results .= ");";
- }
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header('Pragma: public');
- //header('Transfer-Encoding: chunked');
- header('Content-Type: application/octet-stream');
- header('Content-Length: '.strlen($this->results));
- header('Content-Disposition: attachment; filename="'.$table_name.'.sql"');
- echo $this->results;
- } else {
- trigger_error('Error while dumping table data due the following error : '.mysql_error(),E_USER_ERROR);
- }
- } else {
- trigger_error('Cannot Execute SHOW CREATE TABLE due the following error : '.mysql_error(),E_USER_ERROR);
- }
- }
- }
- }
- }
- ?>