1

我的 Sql 查询返回空结果集。格式有没有错误。我在查询中使用了一些 php 变量..

public function getBlockDate($month,$year){
    $connection = db::factory('mysql');
    $time_from="00-00-00";
    $time_to="23-59-59";
    $sql = 'select * from blocks WHERE date LIKE "'.$year.'-'.$month.'%" AND (time_From="'.$time_from.'" AND time_to="'.$time_to.'")';

    return $connection->getArray($sql);
}

*这里 time_from 和 time_to 表列是时间类型*

4

3 回答 3

0

更改您的 SQL 查询

$sql = 'select * from blocks WHERE date LIKE "'.$year.'-'.$month.'%" AND (time_From="'.$time_from.'" AND time_to="'.$time_to.'")';

'缺少单引号$time_from$time_to

已编辑的查询

$time_from="00:00:00";
$time_to="23:59:59";
$sql = 'select * from blocks WHERE date LIKE "'.$year.'-'.$month.'%" BETWEEN (time_From="'.$time_from.'" AND time_to="'.$time_to.'")';

也改变你和变量中的-with:$time_from$time_to

于 2013-07-29T09:55:03.763 回答
0

这应该适合你。您没有将配额放在 AND 子句中。此外,在查询中使用 LIKE 时,请务必正确放置通配符。在您的情况下,您正在查找日期列中以年月开头的记录,然后就是您的时间。

 $time_from="00-00-00";
 $time_to="23-59-59";
 $sql = "SELECT * FROM blocks WHERE date LIKE '".$year."-".$month."%' AND (time_From='".$time_from."' AND time_to='".$time_to."')";
于 2013-07-29T10:05:10.573 回答
0

将日期和时间转换为通用格式,然后使用它进行查询。

于 2013-07-29T10:41:10.000 回答