最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Doing $wpdb->get_results returns NULL, doing the same query in my DB returns correct value

programmeradmin3浏览0评论

I have this query to do in my code:

  function get_item($id){
    global $wpdb;

    $post_id_query = "SELECT * FROM wp_postmeta WHERE post_id = ". $id ." AND meta_key LIKE '%main%'";
    $call_post_id_call = $wpdb->get_results($wpdb->prepare($post_id_query));

    return $call_post_id_call;
  }

  $main_item = get_item(34487);

If I run SELECT * FROM wp_postmeta WHERE post_id = 34487 AND meta_key LIKE '%main%' on my DB it works...

I'm not very keen on wordpress to be honest.

I have this query to do in my code:

  function get_item($id){
    global $wpdb;

    $post_id_query = "SELECT * FROM wp_postmeta WHERE post_id = ". $id ." AND meta_key LIKE '%main%'";
    $call_post_id_call = $wpdb->get_results($wpdb->prepare($post_id_query));

    return $call_post_id_call;
  }

  $main_item = get_item(34487);

If I run SELECT * FROM wp_postmeta WHERE post_id = 34487 AND meta_key LIKE '%main%' on my DB it works...

I'm not very keen on wordpress to be honest.

Share Improve this question asked Apr 29, 2020 at 13:54 user2026121user2026121 32 bronze badges 1
  • Try to run print $wpdb->last_query; before the return call and check the exact query being executed. Also, couldn't see any issues in the script, it should work if the query works in you DB. – Karthick Commented Apr 29, 2020 at 15:06
Add a comment  | 

1 Answer 1

Reset to default 0

The following script will help you to identify if there any error exists in the query.

$wpdb -> show_errors ();
$call_post_id_call = $wpdb->get_results($wpdb->prepare($post_id_query));
$wpdb -> print_error ();
发布评论

评论列表(0)

  1. 暂无评论