Wordpress $wpdb->get_results() 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14800411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress $wpdb->get_results() query
提问by danyo
I am trying to run a mysql_fetch_array via Wordpress. I found out the best way to do this is explained here: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
我正在尝试通过 Wordpress 运行 mysql_fetch_array。我发现最好的方法是在这里解释:http: //codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
Here is my query below:
这是我的查询如下:
$sql = "SELECT * FROM wp_reminders WHERE reminder LIKE '$today'";
$result = $wpdb->get_results($sql) or die(mysql_error());
foreach( $result as $results ) {
echo $result->name;
}
The above is not pulling in any results at all, even though the data does exist. Any ideas what I am doing wrong?
即使数据确实存在,上述内容也根本没有得出任何结果。任何想法我做错了什么?
回答by danyo
the problem was the following:
问题如下:
echo $result->name;
should be:
应该:
echo $results->name;
回答by jharrell
The 'foreach' loop and the initial var statement for 'result = $wpdb->...' should be results.
'foreach' 循环和 'result = $wpdb->...' 的初始 var 语句应该是结果。
$sql = "SELECT * FROM wp_reminders WHERE reminder LIKE '$today'";
$results = $wpdb->get_results($sql) or die(mysql_error());
foreach( $results as $result ) {
echo $result->name;
}
The logic behind this is that you would be gathering all results from the get_results() function and then looping through them as such: (read it out loud - the logic is enforced)
这背后的逻辑是,您将从 get_results() 函数中收集所有结果,然后像这样循环遍历它们:(大声朗读 - 执行逻辑)
foreach ( $ofTheMassiveList as $aSingleResult ) {
echo $aSingleResult->name;
}