ezSQL_pdo::query PHP Method

query() public method

Basic Query - see docs for more detail
public query ( $query )
    function query($query)
    {
        // For reg expressions
        $query = str_replace("/[\n\r]/", '', trim($query));
        // initialise return
        $return_val = 0;
        // Flush cached values..
        $this->flush();
        // Log how the function was called
        $this->func_call = "\$db->query(\"{$query}\")";
        // Keep track of the last query for debug..
        $this->last_query = $query;
        $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->dsn, $this->user, $this->password);
            // No existing connection at this point means the server is unreachable
            if (!isset($this->dbh) || !$this->dbh) {
                return false;
            }
        }
        // Query was an insert, delete, update, replace
        if (preg_match("/^(insert|delete|update|replace|drop|create)\\s+/i", $query)) {
            // Perform the query and log number of affected rows
            $this->rows_affected = $this->dbh->exec($query);
            // If there is an error then take note of it..
            if ($this->catch_error()) {
                return false;
            }
            $is_insert = true;
            // Take note of the insert_id
            if (preg_match("/^(insert|replace)\\s+/i", $query)) {
                $this->insert_id = @$this->dbh->lastInsertId();
            }
            // Return number fo rows affected
            $return_val = $this->rows_affected;
        } else {
            // Perform the query and log number of affected rows
            $sth = $this->dbh->query($query);
            // If there is an error then take note of it..
            if ($this->catch_error()) {
                return false;
            }
            $is_insert = false;
            $col_count = $sth->columnCount();
            for ($i = 0; $i < $col_count; $i++) {
                $this->col_info[$i] = new stdClass();
                if ($meta = $sth->getColumnMeta($i)) {
                    $this->col_info[$i]->name = $meta['name'];
                    $this->col_info[$i]->type = !empty($meta['native_type']) ? $meta['native_type'] : 'undefined';
                    $this->col_info[$i]->max_length = '';
                } else {
                    $this->col_info[$i]->name = 'undefined';
                    $this->col_info[$i]->type = 'undefined';
                    $this->col_info[$i]->max_length = '';
                }
            }
            // Store Query Results
            $num_rows = 0;
            while ($row = @$sth->fetch(PDO::FETCH_ASSOC)) {
                // Store relults as an objects within main array
                $this->last_result[$num_rows] = (object) $row;
                $num_rows++;
            }
            // 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;
    }

Usage Example

 /**
  * Perform mySQL query
  *
  * Added to the original function: logging of all queries
  *
  * @since 1.7
  */
 function query($query)
 {
     // Keep history of all queries
     $this->debug_log[] = $query;
     // Original function
     return parent::query($query);
 }
All Usage Examples Of ezSQL_pdo::query