gcdb::get_results()

最后更新于:2021-11-26 08:53:02

gcdb::get_results( string$query=null, string$output=OBJECT)

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

参数

$query

(string) (Optional) SQL query.

Default value: null

$output

(string) (Optional) Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. 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_K, return an associative array of row objects keyed by the value of each row’s first column’s value. Duplicate keys are discarded.

Default value: OBJECT

响应

(array|object|null) Database query results.

源文件

文件: gc-includes/gc-db.php

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

		if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
			$this->check_current_query = false;
		}

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

		$new_array = array();
		if ( OBJECT === $output ) {
			// 响应 an integer-keyed array of row objects.
			return $this->last_result;
		} elseif ( OBJECT_K === $output ) {
			// 响应 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 = get_object_vars( $row );
					$key        = array_shift( $var_by_ref );
					if ( ! isset( $new_array[ $key ] ) ) {
						$new_array[ $key ] = $row;
					}
				}
			}
			return $new_array;
		} elseif ( ARRAY_A === $output || ARRAY_N === $output ) {
			// 响应 an integer-keyed array of...
			if ( $this->last_result ) {
				foreach ( (array) $this->last_result as $row ) {
					if ( ARRAY_N === $output ) {
						// ...integer-keyed row arrays.
						$new_array[] = array_values( get_object_vars( $row ) );
					} else {
						// ...column name-keyed row arrays.
						$new_array[] = get_object_vars( $row );
					}
				}
			}
			return $new_array;
		} elseif ( strtoupper( $output ) === OBJECT ) {
			// Back compat for OBJECT being previously case-insensitive.
			return $this->last_result;
		}
		return null;
	}
$query = <<<SQL
SELECT
       p.ID, m.meta_key, m.meta_value
FROM
     $gcdb->posts p
     LEFT JOIN
     $gcdb->postmeta m ON p.ID = m.post_id
     WHERE
           p.post_type IN ( 'post', 'page' )
           AND p.post_status = 'publish'
           AND ( m.meta_key IN ( '_yoast_gcseo_title', '_yoast_gcseo_metadesc', '_yoast_gcseo_meta-robots-noindex', 
           '_yoast_gcseo_meta-robots-nofollow' )
           OR 1 = 1 )
SQL;
        
/** @var array $result this will give us the ID and the other meta_field if any of all post types selected */
$result = $gcdb->get_results( $query, ARRAY_A );