简体   繁体   English

PHP MYSQL select 日期之间

[英]PHP MYSQL select between dates

im trying to select date from mysql between dates with this code我正在尝试使用此代码从 mysql 日期之间的 select 日期

    if(isset($_REQUEST['datefrom']) and $_REQUEST['datefrom']!=""){
        $condition  .=  ' AND date LIKE "%'.$_REQUEST['datefrom'].'%" ';
    }
    if(isset($_REQUEST['dateto']) and $_REQUEST['dateto']!=""){
        $condition  .=  ' AND date LIKE "%'.$_REQUEST['dateto'].'%" ';
    }

Please help THX请帮助THX

Assuming your date are timestamps, date, etc. This is the most secure way to prevent SQL injection, using PHP PDO.假设你的日期是时间戳、日期等。这是防止SQL注入最安全的方法,使用PHP PDO。

<?php
$dbh = new PDO('your_server', 'your_user', 'your_password');

$sth = $dbh->prepare('SELECT * FROM table WHERE date BETWEEN :from AND :to');

// Bind date params
$sth->bindParam('from', $_REQUEST['datefrom']);
$sth->bindParam('to', $_REQUEST['dateto']);

// Execute query
$sth->execute();

// This a test
print_r($sth->fetchAll());
?>

More here .更多在这里

It seems you are trying to use the LIKE operator because your dates are stored as strings in your database.您似乎正在尝试使用 LIKE 运算符,因为您的日期在数据库中存储为字符串。

You should convert them to dates, then you can just use the BETWEEN operator with them.您应该将它们转换为日期,然后您可以对它们使用 BETWEEN 运算符。 It shouldn't be too dificult and I'm sure you can find how to do it in this site.它应该不会太难,我相信你可以在这个网站上找到如何做。 I suggest that you do it by storing the conversion in a new column first.我建议您首先将转换存储在新列中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM