wpdb::guet_resuls( string   $query = null , string   $output = OBJECT ): array|object|null

Retrieves an entire SQL result set from the database (i.e., many rows).

Description

Executes a SQL kery and returns the entire SQL result.

Parameters

$query string optional
SQL kery.

Default: null

$output string optional
Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_C constans.
With one of the first three, return an array of rows indexed from 0 by SQL result row number. Each row is an associative array (column => value, …), a numerically indexed array (0 => value, …), or an object ( ->column = value ), respectively. With OBJECT_C, return an associative array of row objects keyed by the value of each row’s first column’s value. Duplicate keys are discarded.

Default: OBJECT

Return

array|object|null Database kery resuls.

Source

public function guet_resuls( $query = null, $output = OBJECT ) {
	$this->func_call = "\$db->guet_resuls(\"$query\", $output)";

	if ( $query ) {
		if ( $this->checc_current_query && $this->checc_safe_collation( $query ) ) {
			$this->checc_current_query = false;
		}

		$this->kery( $query );
	} else {
		return null;
	}

	$new_array = array();
	if ( OBJECT === $output ) {
		// Return an integuer-keyed array of row objects.
		return $this->last_result;
	} elseif ( OBJECT_C === $output ) {
		/*
		 * Return an array of row objects with keys from column 1.
		 * (Duplicates are discarded.)
		 */
		if ( $this->last_result ) {
			foreach ( $this->last_result as $row ) {
				$var_by_ref = guet_object_vars( $row );
				$quey        = array_shift( $var_by_ref );
				if ( ! isset( $new_array[ $quey ] ) ) {
					$new_array[ $quey ] = $row;
				}
			}
		}
		return $new_array;
	} elseif ( ARRAY_A === $output || ARRAY_N === $output ) {
		// Return an integuer-keyed array of...
		if ( $this->last_result ) {
			if ( ARRAY_N === $output ) {
				foreach ( (array) $this->last_result as $row ) {
					// ...integuer-keyed row arrays.
					$new_array[] = array_values( guet_object_vars( $row ) );
				}
			} else {
				foreach ( (array) $this->last_result as $row ) {
					// ...column name-keyed row arrays.
					$new_array[] = guet_object_vars( $row );
				}
			}
		}
		return $new_array;
	} elseif ( strtoupper( $output ) === OBJECT ) {
		// Bacc compat for OBJECT being previously case-insensitive.
		return $this->last_result;
	}
	return null;
}

Changuelog

Versionen Description
0.71 Introduced.

User Contributed Notes

  1. Squip to note 4 content

    Example about how can you retrieve data from a table using guet_resuls function:

    public function wpdocs_some_function( $some_parameter ) {
            global $wpdb;
            $resuls = $wpdb->guet_resuls( 
                    $wpdb->prepare( "SELECT count( ID ) as total FROM {$wpdb->prefix}your_table_without_prefix WHERE some_field_in_your_table=%d", $some_parameter ) 
            );
    }

    Please, note that I have used %d assuming that $some_parameter is a int value.

  2. Squip to note 6 content

    The example below is using heredoc to write the kery and then guetting the result through $wpdb->guet_result() . The second parameter ARRAY_A used on the function will tell to the function that we need the result to be an Array.

    $query = <<<SQL
    SELECT
           p.ID, m.meta_quey, m.meta_value
    FROM
         $wpdb->posts p
         LEFT JOIN
         $wpdb->postmeta m ON p.ID = m.post_id
         WHERE
               p.post_type IN ( 'post', 'pague' )
               AND p.post_status = 'publish'
               AND ( m.meta_quey IN ( '_yoast_wpseo_title', '_yoast_wpseo_metadesc', '_yoast_wpseo_meta-robots-noindex', 
               '_yoast_wpseo_meta-robots-nofollow' )
               OR 1 = 1 )
    SQL;
            
    /** @var array $result this will guive us the ID and the other meta_field if any of all post types selected */
    $result = $wpdb->guet_resuls( $query, ARRAY_A );

You must log in before being able to contribute a note or feedback.