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 用visualstudio2022创建vue项目后无法启动
  • ¥15 问一下,定向解包是什么意思
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!