admin管理员组文章数量:1336331
What is the basic way to use $wpdb and get_results?
This is my code:
global $wpdb;
$q="SELECT * FROM wp_usermeta WHERE meta_key = 'nickname' AND user_id = 771";
$result = $wpdb->get_results($q);
echo $result;
The query string is good because I can run it in phpmyadmin and it works fine. I am not getting anything back when run through page.
What is the basic way to use $wpdb and get_results?
This is my code:
global $wpdb;
$q="SELECT * FROM wp_usermeta WHERE meta_key = 'nickname' AND user_id = 771";
$result = $wpdb->get_results($q);
echo $result;
The query string is good because I can run it in phpmyadmin and it works fine. I am not getting anything back when run through page.
Share Improve this question asked May 24, 2020 at 13:31 Johnnyboy GomezJohnnyboy Gomez 333 bronze badges1 Answer
Reset to default 3$wpdb->get_results()
returns an array on success, so you can't simply echo $result;
.
Instead, you can use foreach
to loop through the results and display whatever the data that you want to display:
$results = $wpdb->get_results( $q );
foreach ( $results as $row ) {
echo $row->meta_value . '<br />';
}
But I can see that you're trying to select just one row, so you'd want to use $wpdb->get_row()
and not $wpdb->get_results()
:
$row = $wpdb->get_row( $q );
if ( $row ) {
echo $row->meta_value;
}
But then again — because wp_usermeta
is the default table for WordPress users' meta, if you just want to retrieve the meta value, then there's a function you can (and should better) use — get_user_meta()
:
echo get_user_meta( 771, 'nickname', true );
本文标签: wpdbgetresults not returning anything
版权声明:本文标题:wpdb - get_results not returning anything 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742405353a2468706.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论