db_addr = $db_addr; $this->db_user = $db_user; $this->db_pass = $db_pass; $this->querycount = 0; if ( $use_pconnect ) { $this->link = @mysql_pconnect($db_addr, $db_user, $db_pass); } else { $this->link = @mysql_connect($db_addr, $db_user, $db_pass); } if ( $this->link ) { $q = @mysql_query("SET NAMES 'utf8'", $this->link); @mysql_free_result($q); if ( $db_name != '' ) { $this->db_name = $db_name; if ( !@mysql_select_db($db_name, $this->link) ) { @mysql_close($this->db_connect_id); $this->error("Could not select database '$db_name'. Check that the value of DB_NAME in config.php is set correctly."); } } return $this->link; } else { $this->error('Could not connect to database server. Check that the values of DB_ADDR, DB_USER and DB_PASS in config.php are set correctly.'); } } function data_seek($row_number, $query_id = 0) { if ( !$query_id ) { $result = $this->last_result; } if ( $query_id ) { return @mysql_data_seek($query_id, $row_number); } return false; } function fetch_array($query_id = 0) { if ( !$query_id ) { $query_id = $this->last_result; } if ( $query_id ) { return @mysql_fetch_array($query_id); } return false; } function fetch_row($query_id = 0) { if ( !$query_id ) { $query_id = $this->last_result; } if ( $query_id ) { return @mysql_fetch_row($query_id); } return false; } function fetch_row_set($query_id = 0) { if ( !$query_id ) { $query_id = $this->last_result; } if ( $query_id ) { $rowset = array(); while ( $row = $this->fetch_array($query_id) ) $rowset[] = $row; return $rowset; } return false; } function free_result($query_id = 0) { if ( !$query_id ) { $query_id = $this->last_result; } if ( $query_id ) { return @mysql_free_result($query_id); } return false; } function insert_id() { return $this->last_insert_id; } function num_rows($query_id = 0) { if ( !$query_id ) { $query_id = $this->last_result; } if ( $query_id ) { return @mysql_num_rows($query_id); } return false; } function calc_rows() { return $this->last_calc_rows; } function query($query, $showerror=true, $calcrows=false) { $this->last_query = $query; $starttime = microtime(true); if($calcrows == true) { /* Add sql_calc_found_rows to this query */ $query = preg_replace('/select/i', 'select sql_calc_found_rows', $query, 1); } $this->last_result = @mysql_query($query, $this->link); $endtime = microtime(true); $this->last_insert_id = @mysql_insert_id($this->link); if($calcrows == true) { $calc_result = @mysql_query("select found_rows() as rowcount"); if($row = mysql_fetch_assoc($calc_result)) { $this->last_calc_rows = $row['rowcount']; } } $this->querycount++; if ( defined('DB_DEBUG') && DB_DEBUG == true ) { echo "

$query

"; } if ( $this->last_result ) { if($this->profile) { $backtrace = debug_backtrace(); $profilequery = "insert into hlstats_sql_web_profile (source, run_count, run_time) values ". "('".basename($backtrace[0]['file']).':'.$backtrace[0]['line']."',1,'".($endtime-$starttime)."')" ."ON DUPLICATE KEY UPDATE run_count = run_count+1, run_time=run_time+".($endtime-$starttime); @mysql_query($profilequery, $this->link); } return $this->last_result; } else { if ($showerror) { $this->error('Bad query.'); } else { return false; } } } function result($row, $field, $query_id = 0) { if ( !$query_id ) { $query_id = $this->last_result; } if ( $query_id ) { return @mysql_result($query_id, $row, $field); } return false; } function escape($string) { if ( $this->link ) { return @mysql_real_escape_string($string, $this->link); } else { return @mysql_real_escape_string($string); } } function error($message, $exit=true) { error( "Database Error
\n
\n" . "Server Address: $this->db_addr
\n" . "Server Username: $this->db_user

\n" . "Error Diagnostic:
\n$message

\n" . "Server Error: (" . @mysql_errno() . ") " . @mysql_error() . "

\n" . "Last SQL Query:
\n
$this->last_query
", $exit ); } } ?>