admin管理员组

文章数量:1315971

I'm trying to retrieve information on my database. I wanted to display all pages using this statement, but I'm getting a blank ARRAY

global $wpdb;
$result = $wpdb->get_results (
        "
        SELECT * 
        FROM  $wpdb->wp_posts 
        WHERE post_type =  'page'
        "
        );

echo $result; // display data

Output:

  ARRAY

EDIT: After changing below suggestions, I'm now using this. but I still don't get any results:

global $wpdb;

    $posts = $wpdb->wp_posts;
    $result = $wpdb->get_results( " SELECT * FROM  $posts WHERE 'post_type' =  'page' "  );

    foreach ($result as $page) {
            echo $page->ID.'<br/>';

    }

I'm trying to retrieve information on my database. I wanted to display all pages using this statement, but I'm getting a blank ARRAY

global $wpdb;
$result = $wpdb->get_results (
        "
        SELECT * 
        FROM  $wpdb->wp_posts 
        WHERE post_type =  'page'
        "
        );

echo $result; // display data

Output:

  ARRAY

EDIT: After changing below suggestions, I'm now using this. but I still don't get any results:

global $wpdb;

    $posts = $wpdb->wp_posts;
    $result = $wpdb->get_results( " SELECT * FROM  $posts WHERE 'post_type' =  'page' "  );

    foreach ($result as $page) {
            echo $page->ID.'<br/>';

    }
Share Improve this question edited Sep 19, 2016 at 5:44 Ethan Rævan 4,0295 gold badges27 silver badges55 bronze badges asked Aug 19, 2013 at 13:10 user1933824user1933824 7112 gold badges7 silver badges9 bronze badges 1
  • Try wrapping $wpdb->wp_posts with curly braces, ie. {$wpdb->wp_posts}.. – t31os Commented Aug 19, 2013 at 13:40
Add a comment  | 

4 Answers 4

Reset to default 30
global $wpdb;

$result = $wpdb->get_results ( "
    SELECT * 
    FROM  $wpdb->posts
        WHERE post_type = 'page'
" );

foreach ( $result as $page )
{
   echo $page->ID.'<br/>';
   echo $page->post_title.'<br/>';
}

You have a slight misunderstanding:

When calling $wpdb, you get a list of properties that contain the core names of the tables:

// The custom prefix from wp-config.php
// only needed for custom tables
$wpdb->prefix

// Tables where you don't need a prefix: built in ones:
$wpdb->posts
$wpdb->postmeta
$wpdb->users

So your final query would look like this:

$wpdb->get_results( "SELECT * FROM {$wpdb->posts} WHERE post_type = 'page'" );

Try the following code. I faced the similar problem and solved it by removing $wpdb from 'FROM' field.

global $wpdb;
$result = $wpdb->get_results (
            "
            SELECT * 
            FROM  wp_posts 
            WHERE post_type =  'page'
            "
            );

echo $result; // display data

By "blank Array" do you mean an 'empty array' or is the output 'ARRAY'. If it's the latter then, it is the expected output. You need to loop through that array and display results accordingly.

Reference: http://codex.wordpress/Class_Reference/wpdb#SELECT_Generic_Results

本文标签: databasegetresults using wpdb