0

This is my current PHP code:

$sql = 'SELECT * from comments where post_id_fk=$post_id';

$users = $db->prepare($sql);
$users->execute();

while($row = pg_fetch_array($users, 0, PGSQL_ASSOC))

For some reason, I keep getting the following error:

Warning: pg_fetch_array() expects parameter 1 to be resource, object given in /home/se212004/public_html/content.html on line 39`

The line number refers to the line with the while loop. I have tried to fix it several times, but I cannot get it to work.

1
  • You have an error in your query. Use pg_result_error() to see what it is. Commented Apr 13, 2013 at 21:10

1 Answer 1

1

You are using PDO for preparing and executing the statement, and I believe you should also use it for fetching the results. So, basically, you would have to do something like this:

$sql = 'SELECT * from comments where post_id_fk=$post_id';

$users = $db->prepare($sql);
$users->execute();

$results = $users->fetchAll();

Or you could do:

$sql = 'SELECT * from comments where post_id_fk=$post_id';

$users = $db->prepare($sql);
$users->execute();

while ($row = $users->fetch(PDO::FETCH_ASSOC)){
      // do something with each row
}
Sign up to request clarification or add additional context in comments.

Comments

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.