duanche5149 2014-10-10 14:16
浏览 36
已采纳

Where子句和Order By未在WordPress中按预期返回

I am positive I am missing something here.

I have a MySQL database table setup with: kdate, kday, kmonth, and kontent

kdate is set with an expires date (admin input) formatted as 01-01-2014 date(m-d-Y)

I am trying to get dates older than today to not show on execution of the script.

global $wpdb;
$today = date('m-d-y');
$result = $wpdb->get_results( "SELECT * FROM wp_kaltable WHERE kdate >= $today ORDER BY kdate ASC    LIMIT 6 ");

//echo "<pre>"; print_r($result); echo "</pre>";

foreach($result as $row)
{

echo "<div class='kalcontain'><div class='kal'><div class='kdate'><p class='kmonth'>".$row->kmonth."</p><p class='kday'>" .$row->kday."</div><div class='kontent'><p class='ktext'>".$row->kontent. "</p></div><div class'clear'></div></div></div>";

}

When I execute this script, I get all data, even the ones with dates less than today's date. Also, when using ORDER BY, it is ordering by month and day, but not year.

All assistance is appreciated.

(*This code is for testing purposes only)

  • 写回答

2条回答 默认 最新

  • duanmengsuo9302 2014-10-10 14:19
    关注

    Try instead:

    "SELECT * FROM wp_kaltable WHERE kdate >= '$today' ORDER BY kdate ASC    LIMIT 6 "
    

    The single quotes around the $today are important.

    If kdate is date format then you could just use CURDATE() from mysql so you don't have to muck around in php:

    "SELECT * FROM wp_kaltable WHERE kdate >= CURDATE() ORDER BY kdate ASC    LIMIT 6 "
    

    If kdate is not date format, which it may not be since your ORDER BY isn't working correctly, then neither of those SQL statements will work. You will need to convert to date format in order to the >=

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

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab