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
|
4 Answers
Reset to default 30global $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
版权声明:本文标题:database - get_results using wpdb 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741996257a2410105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$wpdb->wp_posts
with curly braces, ie.{$wpdb->wp_posts}
.. – t31os Commented Aug 19, 2013 at 13:40