使用wpdb
-
-
尝试用花括号将`$ wpdb-> wp_posts`包装.{$ wpdb-> wp_posts} ..Try wrapping `$wpdb->wp_posts` with curly braces, ie. `{$wpdb->wp_posts}`..
- 0
- 2013-08-19
- t31os
-
4 个回答
- 投票数
-
- 2013-08-19
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/>'; }
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/>'; }
-
您好@balamurugan,我尝试了您的回答,但我仍然没有得到任何结果.您可以在上方看到我的[EDIT]部分.hi @balamurugan, i tried your answer but im still not getting any results. you can see my [EDIT] part above.
- 0
- 2013-08-19
- user1933824
-
实际上,您得到的是什么,以及要从我的代码中删除....我测试了它并获取所有页面IDactually what u r getting and do u remove ... from my code. i tested it and getting all page id
- 0
- 2013-08-19
- Balas
-
即时通讯使用我的[edit]部分,如我的原始文章上方所示. 我尝试了`echo $ result`只是为了确保我从查询中检索到的数据是print`Array`.当我使用`echo $page-> ID`我什么都没得到.我真的不确定为什么..im using my [edit] part as seen above my original post. i tried `echo $result` just to make sure that im retrieving data from the `query` what i get is print `Array`. when i use `echo $page->ID` i dont get anything. im really not sure why..
- 0
- 2013-08-19
- user1933824
-
您只需简单地完全复制并粘贴该代码即可.这是获得结果的全部步骤.you just simply copy & paste that code completely. That's all to do to get the result.
- 0
- 2013-08-19
- Balas
-
是的,它有效!当我尝试查看我的代码和你的代码时,我看到的唯一区别是这部分`$tablename=$ wpdb->prefix.'posts';`这部分不在法典文档中.您能告诉我为什么会起作用吗?yes, it worked! when i try to review my code and yours, the only difference i saw is this part `$tablename = $wpdb->prefix.'posts';` this part wasnt in the codex documentation. can you explain to me why it works?
- 0
- 2013-08-19
- user1933824
-
$ wpdb->prefix=wp_(如数据库表前缀),将与数据库中的wp_posts相同.将来,您可以将yourname作为前缀.这样它就不会影响这种代码.并尝试始终使用这种方式.$wpdb->prefix = wp_ ( as in database table prefix) which will be as wp_posts in database. in future u can give yourname as prefix. so that it will not affect this kind of code. And try always use this way.
- 0
- 2013-08-19
- Balas
-
- 2013-08-19
您有一点误会:
调用
$wpdb
时,将获得包含表核心名称的属性列表:// 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
因此您的最终查询看起来像 这样:
$wpdb->get_results( "SELECT * FROM {$wpdb->posts} WHERE post_type = 'page'" );
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'" );
-
为此+1,谢谢.但是我需要赞扬首先回复我的人,他已经提供了正确的答案,我只是无法听从他的指示.+1 for this, thank you. but i needed to give credit the person who responded to me first, he already provided the correct answer, i was just wasnt able to follow his instruction.
- 1
- 2013-08-19
- user1933824
-
当然.旁注:正如我所说,`$ wpdb->prefix`不应用于内置表.只需直接致电即可.固定这也是他的答案.Sure. Sidenote: As I stated, the `$wpdb->prefix` shouldn't be used for built-in tables. Just call them directly. Fixed this is his answer as well.
- 0
- 2013-08-19
- kaiser
-
- 2014-07-16
尝试以下代码.我遇到了类似的问题,并通过从" FROM"字段中删除$ wpdb来解决了这个问题.
global $wpdb; $result = $wpdb->get_results ( " SELECT * FROM wp_posts WHERE post_type = 'page' " ); echo $result; // display data
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
-
- 2013-08-19
"空白数组"是指"空数组"还是输出" ARRAY".如果是后者,则为预期输出.您需要遍历该数组并相应显示结果.
参考: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
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.org/Class_Reference/wpdb#SELECT_Generic_Results
我正在尝试检索数据库中的信息.我想使用此语句显示所有
pages
,但是我得到了一个空白的ARRAY
输出:
编辑:更改以下建议后,我现在正在使用它.但我仍然没有任何结果: