查询适用于postgresql但不适用于php

时间:2013-06-14 05:00:54

标签: php postgresql

让我们说,我在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
发生了什么事?为什么我不能获得数据?

1 个答案:

答案 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));