doucan4873 2017-03-30 13:40
浏览 62
已采纳

$ wpdb返回空,包含日期选择

I'm working on a WordPress calendar plugin, with possibility to add events attached to one date. Events are stored in a custom database that works fine.

But I've got troubles with a function in wich I use a '$wpdb->get_results' This function need a date as parameter. This is my code :

function get_events_list($day) {

    global $wpdb
    $table = $wpdb->prefix . "mytable";

    $events_list = $wpdb->get_results( "SELECT * FROM $table WHERE date = $day)", ARRAY_A );

    var_dump($events_list);

}

'var_dump($events_list)' returns an empty array, but I've got entries in the table.

I tried the function with no paramater, entering a well-formated date directly in the sql request, like that :

function get_events_list() {
    $events_list = $wpdb->get_results( "SELECT * FROM $table WHERE date = '2017-03-30')", ARRAY_A );
}

This works great.

I also did a 'var_dump($wpdb->last_query)', trying to find what happen.

Tested with no parameter (hard-coded '2017-03-30'), wich is working fine, the last_query returns :

'SELECT * FROM wp_mytable WHERE date = '2017-03-30')'

With dynamic paramater, it returns the date without quote :

'SELECT * FROM wp_mytable WHERE date = 2017-03-30)'

I think I'm pointing the problem but cannot resolve it (I tried a lot of things). Could Someone help me please ? Thank you :)

  • 写回答

2条回答 默认 最新

  • duanan2732 2017-03-30 14:42
    关注

    Hello_ mate,

    first I dont't understand how could this ('SELECT * FROM wp_mytable WHERE date = '2017-03-30')') query work, this final bracket looks very very strange to me :).

    Anyway if your query is working fine then simply change:

    $events_list = $wpdb->get_results( "SELECT * FROM $table WHERE date = $day)", ARRAY_A );
    

    to

    $events_list = $wpdb->get_results( "SELECT * FROM $table WHERE date = '$day')", ARRAY_A );
    

    as you see we only put quotes when passing the variable.

    Let me know if this works for you.

    Good Luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line