简体   繁体   中英

Sql Query Not Working with php variables

My Sql Query Returning Empty results set. Is there any Mistake in format. I am Using some php variables inside the query..

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);
}

* Here time_from and time_to table columns are of time type*

Change your SQL query

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

' single quotes are missing over $time_from and $time_to

EDITED QUERY

$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.'")';

also change the - with : in your $time_from and $time_to variables

This should work for you. You didn't put quotas right at the AND clause. Also when using LIKE in your query be sure to place the wildcard correctly. In your case you are looking for records in date column whose start with year-month then there's your time.

 $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."')";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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