Host = $dbhost; $this->Database = $dbname; $this->User = $dbuser; $this->Password = $dbpass; $this->Timeout = $timeout; ini_set('mysql.connect_timeout', $timeout); } function set_DB($dbhost, $dbname, $dbuser, $dbpass) { $this->Host = $dbhost; $this->Database = $dbname; $this->User = $dbuser; $this->Password = $dbpass; } function halt($msg) { echo "DB ERROR : $msg
\n"; echo "MySQL ERROR: $this->Error ($this->Errno)
\n"; die('Session Halted.'); } function connect() { if ($this->Link_ID==null) { $this->Link_ID = mysql_connect($this->Host, $this->User, $this->Password); if (!$this->Link_ID) { //$this->halt('Link_ID == false, connect failed'); $this->Errno = 999; return; } //if (_CHARSET=='UTF-8') mysql_set_charset('utf8'); mysql_set_charset('utf8'); $SelectResult = mysql_select_db($this->Database, $this->Link_ID); if (!$SelectResult) { $this->Errno = mysql_errno($this->Link_ID); $this->Error = mysql_error($this->Link_ID); $this->Link_ID = null; $this->halt('cannot select database '.$this->Database.''); } } } function escape($String) { $this->connect(); return mysql_real_escape_string($String); } function query($Query_String) { $this->connect(); if ($this->Errno!=0) return 0; $this->Query_ID = mysql_query($Query_String, $this->Link_ID); $this->Row = 0; $this->Errno = mysql_errno(); $this->Error = mysql_error(); if (!$this->Query_ID) { $this->halt('Invalid SQL: '.$Query_String); } return $this->Query_ID; } function next_record() { $this->Record = @mysql_fetch_array($this->Query_ID); $this->Row += 1; $this->Errno = mysql_errno(); $this->Error = mysql_error(); $stat = is_array($this->Record); if (!$stat) { @mysql_free_result($this->Query_ID); $this->Query_ID = null; } return $this->Record; } function num_rows() { return mysql_num_rows($this->Query_ID); } function affected_rows() { return mysql_affected_rows($this->Link_ID); } function optimize($tbl_name) { $this->connect(); if ($this->Errno!=0) return; $this->Query_ID = @mysql_query('OPTIMIZE TABLE '.$tbl_name, $this->Link_ID); } function clean_results() { if ($this->Query_ID!=null) { mysql_freeresult($this->Query_ID); $this->Query_ID = null; } } function close() { /* if ($this->Link_ID) { mysql_close($this->Link_ID); $this->Link_ID = null; } */ } function exist_table($table, $lower_case=true) { $ret = false; if ($lower_case) $table = strtolower($table); $this->query('SHOW TABLES'); if ($this->Errno==0) { while (list($db_tbl) = $this->next_record()) { if ($lower_case) $db_tbl = strtolower($db_tbl); if ($db_tbl==$table) { $ret = true; break; } } } /* $this->connect(); if ($this->Errno!=0) return false; $tl = mysql_list_tables($this->Database, $this->Link_ID); while($row=mysql_fetch_row($tl)) { if (in_array($table, $row)) { $ret = true; break; } }*/ return $ret; } function exist_field($table, $field, $lower_case=true) { $ret1 = false; $ret2 = false; if ($lower_case) $cmp_table = strtolower($table); else $cmp_table = $table; $this->query('SHOW TABLES'); if ($this->Errno==0) { while (list($db_tbl) = $this->next_record()) { if ($lower_case) $db_tbl = strtolower($db_tbl); if ($db_tbl==$cmp_table) { $ret1 = true; break; } } } if ($ret1) { $this->query('SHOW COLUMNS FROM '.$table); if ($this->Errno==0) { while (list($db_fld) = $this->next_record()) { if ($db_fld==$field) { $ret2 = true; break; } } } } return $ret2; } function get_update_time($table, $unixtime=true) { $update = ""; if ($unixtime) $update = 0; $this->query("SHOW TABLE STATUS WHERE name='$table'"); if ($this->Errno==0) { $table_status = $this->next_record(); $update = $table_status['Update_time']; if ($unixtime) { if ($update!="") $update = strtotime($update); else $update = 0; } } return $update; } // // Lock // function lock_table($table, $mode='write') { $this->query("LOCK TABLES ".$table." ".$mode); } function unlock_table() { $this->query("UNLOCK TABLES"); } // // Timeout // function set_default_timeout($tm) { ini_set('mysql.connect_timeout', $tm); $this->Timeout = $tm; } function set_temp_timeout($tm) { ini_set('mysql.connect_timeout', $tm); } function reset_timeout() { ini_set('mysql.connect_timeout', $this->Timeout); } } ?>