0

Let's say, I I have this query in my postgresql.

SELECT * FROM logs WHERE user_id = 8328 AND punchin::date = '2013-06-11';

it returns successful.

But when I implent it in 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

what happened? how come I cant get the data?

2
  • Is that all of the php? do you connect to database first? What's the value of $user_id? Commented Jun 14, 2013 at 5:04
  • yes. I tried connecting the database. the value of the variables arer the same as the data given on top of it. $user_id = 8328 and $date = '2013-06-11' Commented Jun 14, 2013 at 5:09

1 Answer 1

1

Try change

$sql = "SELECT * FROM logs WHERE user_id = $user_id AND punchin::date = $date";

to

$sql = "SELECT * FROM logs WHERE user_id = '$user_id' AND DATE(punchin) = '$date'";

Better yet use parameters

$sql = "SELECT * FROM logs WHERE user_id = $1 AND CAST(punchin AS DATE) = $2";
$result = pg_query_params($sql, array($user_id, $date));
Sign up to request clarification or add additional context in comments.

1 Comment

@user2429302 Did it help? Do you need more help with your question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.