让我们说,我在postgresql中有这个查询。
SELECT * FROM logs WHERE user_id = 8328 AND punchin::date = '2013-06-11';
它返回成功。
但是当我在PHP中实现它时:
$sql = "SELECT * FROM logs WHERE user_id = $user_id AND punchin::date = $date";
$record = pg_query($sql);
$row = pg_fetch_array($record);
var_dump($row) //false
发生了什么事?为什么我不能获得数据?
答案 0 :(得分:1)
尝试更改
$sql = "SELECT * FROM logs WHERE user_id = $user_id AND punchin::date = $date";
到
$sql = "SELECT * FROM logs WHERE user_id = '$user_id' AND DATE(punchin) = '$date'";
更好的是使用参数
$sql = "SELECT * FROM logs WHERE user_id = $1 AND CAST(punchin AS DATE) = $2";
$result = pg_query_params($sql, array($user_id, $date));