Beispiel #1
0
 /**
  * Perform a MySQL database query, using current database connection.
  *
  * More information can be found on the codex page.
  *
  * @since 0.71
  *
  * @param string $query Database query
  * @return int|false Number of rows affected/selected or false on error
  */
 public function query($query)
 {
     if (!$this->ready) {
         $this->check_current_query = true;
         return false;
     }
     /**
      * Filter the database query.
      *
      * Some queries are made before the plugins have been loaded
      * and thus cannot be filtered with this method.
      *
      * @since 2.1.0
      *
      * @param string $query Database query.
      */
     $query = apply_filters('query', $query);
     $this->flush();
     // Log how the function was called
     $this->func_call = "\$db->query(\"{$query}\")";
     // If we're writing to the database, make sure the query will write safely.
     if ($this->check_current_query && !$this->check_ascii($query)) {
         $stripped_query = $this->strip_invalid_text_from_query($query);
         // strip_invalid_text_from_query() can perform queries, so we need
         // to flush again, just to make sure everything is clear.
         $this->flush();
         if ($stripped_query !== $query) {
             $this->insert_id = 0;
             return false;
         }
     }
     $this->check_current_query = true;
     // Keep track of the last query for debug..
     $this->last_query = $query;
     $this->_do_query($query);
     // MySQL server has gone away, try to reconnect
     if (!$this->dbh->is_connected()) {
         if ($this->check_connection()) {
             $this->flush();
             $this->_do_query($query);
         } else {
             $this->insert_id = 0;
             return false;
         }
     }
     // If there is an error then take note of it..
     if ($this->last_error = $this->dbh->get_error_message()) {
         // Clear insert_id on a subsequent failed insert.
         if ($this->insert_id && preg_match('/^\\s*(insert|replace)\\s/i', $query)) {
             $this->insert_id = 0;
         }
         $this->print_error();
         return false;
     }
     if (preg_match('/^\\s*(create|alter|truncate|drop)\\s/i', $query)) {
         $return_val = $this->result;
     } elseif (preg_match('/^\\s*(insert|delete|update|replace)\\s/i', $query)) {
         $this->rows_affected = $this->dbh->affected_rows();
         // Take note of the insert_id
         if (preg_match('/^\\s*(insert|replace)\\s/i', $query)) {
             $this->insert_id = $this->dbh->insert_id();
         }
         // Return number of rows affected
         $return_val = $this->rows_affected;
     } else {
         $return_val = $this->num_rows = count($this->result);
     }
     $this->last_result = $this->dbh->get_results();
     return $return_val;
 }