function query($query)
{
// This keeps the connection alive for very long running scripts
if ($this->count(false) >= 500) {
$this->disconnect();
$this->quick_connect($this->dbuser, $this->dbpassword, $this->dbname, $this->dbhost, $this->dbport, $this->encoding);
}
// Initialise return
$return_val = 0;
// Flush cached values..
$this->flush();
// For reg expressions
$query = trim($query);
// Log how the function was called
$this->func_call = "\$db->query(\"{$query}\")";
// Keep track of the last query for debug..
$this->last_query = $query;
// Count how many queries there have been
$this->count(true, true);
// Start timer
$this->timer_start($this->num_queries);
// Use core file cache function
if ($cache = $this->get_cache($query)) {
// Keep tack of how long all queries have taken
$this->timer_update_global($this->num_queries);
// Trace all queries
if ($this->use_trace_log) {
$this->trace_log[] = $this->debug(false);
}
return $cache;
}
// If there is no existing database connection then try to connect
if (!isset($this->dbh) || !$this->dbh) {
$this->connect($this->dbuser, $this->dbpassword, $this->dbhost, $this->dbport);
$this->select($this->dbname, $this->encoding);
// No existing connection at this point means the server is unreachable
if (!isset($this->dbh) || !$this->dbh || $this->dbh->connect_errno) {
return false;
}
}
// Perform the query via std mysql_query function..
$this->result = @$this->dbh->query($query);
// If there is an error then take note of it..
if ($str = @$this->dbh->error) {
$this->register_error($str);
$this->show_errors ? trigger_error($str, E_USER_WARNING) : null;
return false;
}
// Query was an insert, delete, update, replace
if (preg_match("/^(insert|delete|update|start|replace|truncate|drop|create|alter|begin|commit|rollback|set|lock|unlock|call)/i", $query)) {
$is_insert = true;
$this->rows_affected = @$this->dbh->affected_rows;
// Take note of the insert_id
if (preg_match("/^(insert|replace)\\s+/i", $query)) {
$this->insert_id = @$this->dbh->insert_id;
}
// Return number fo rows affected
$return_val = $this->rows_affected;
} else {
$is_insert = false;
// Take note of column info
$i = 0;
while ($i < @$this->result->field_count) {
$this->col_info[$i] = @$this->result->fetch_field();
$i++;
}
// Store Query Results
$num_rows = 0;
while ($row = @$this->result->fetch_object()) {
// Store relults as an objects within main array
$this->last_result[$num_rows] = $row;
$num_rows++;
}
@$this->result->free_result();
// Log number of rows the query returned
$this->num_rows = $num_rows;
// Return number of rows selected
$return_val = $this->num_rows;
}
// disk caching of queries
$this->store_cache($query, $is_insert);
// If debug ALL queries
$this->trace || $this->debug_all ? $this->debug() : null;
// Keep tack of how long all queries have taken
$this->timer_update_global($this->num_queries);
// Trace all queries
if ($this->use_trace_log) {
$this->trace_log[] = $this->debug(false);
}
return $return_val;
}