SQL筛选过去的日期

时间:2012-02-22 00:22:50

标签: php mysql

我早些时候问了一个类似的问题,但没有得到所有的事实。如何修改此查询以过滤掉当前日期之前的所有日期?

$result = $wpdb->get_results("SELECT * FROM wp_location_date WHERE location=$id ORDER BY location_date");

WordPress的function.php文件中的完整(ish)代码。

add_action('wp_ajax_nopriv_fetch_dates', 'get_location_date');
add_action('wp_ajax_fetch_dates', 'get_location_date');
function get_location_date() {
global  $wpdb;
ob_clean();
$id = $_POST['id']; // Get the Request Id;

$result = $wpdb->get_results("SELECT * FROM wp_location_date WHERE location=$id ORDER BY location_date");
foreach ( $result as $value ) {
     $option .= '<option value="'.$value->id.'">';
     $option .= $value->date_coment;
     $option .= '</option>';
    }
 echo '<option value="0" selected="selected">Select Date</option>'.$option;

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

$result = $wpdb->get_results("SELECT * FROM wp_location_date WHERE location=$id and location_date >= curdate() ORDER BY location_date");

另外,请勿在SQL查询中使用原始用户输入!在字符串上使用mysql_real_escape_string,在整数上使用intval,例如

$id = intval($_POST['id']);

$result = $wpdb->get_results('SELECT * FROM wp_location_date WHERE location="'.mysql_real_escape_string($id).'" and location_date >= curdate() ORDER BY location_date');

答案 1 :(得分:1)

location_date的数据类型是什么? 你可以试试这个:

$result = $wpdb->get_results("SELECT * FROM wp_location_date WHERE location=$id AND location_date > current_date - 1 ORDER BY location_date");

有关MySQL Reference Guide的更多信息。