0

So I have this code:

foreach ($_SESSION['basket'] as $item) {
    $ref = $_SESSION['basket'][$counter];

    $result = pg_query ($conn, 'SELECT * from music WHERE ref='.$ref.' ORDER BY artist');
}

This will output the first row fine, however it outputs this Warning: pg_fetch_row() expects parameter 1 to be resource, boolean given if I try to retrieve more than one row. I don't understand how I'm giving a boolean to parameter 1, this is the code on line 46 where it is getting the error: ($row = pg_fetch_row($result))

Thanks in advance

3
  • So where exact code which uses pg_fetch_row? Commented Nov 30, 2014 at 20:19
  • @u_mulder the line that uses that code is the opening of a while statement while ($row = pg_fetch_row($result)) { Commented Nov 30, 2014 at 20:30
  • Okay, the query fails after the first run through the foreach loop, do I need to reset the query or result at the end of the loop so that new data can be retrieved from the database? Commented Nov 30, 2014 at 21:02

3 Answers 3

1

You can use $row =pg_fetch_array($result) and then $row['field_name'] to take values out in the foreach loop.

The error may be because your connection variable $conn does not connect to your database.

Try all possibilities. Thank you.

Sign up to request clarification or add additional context in comments.

Comments

0

If your query fails, pg_query returns FALSE. Also, you should use pg_query_params instead to prevent SQL injection.

Comments

0

Have a look at the examples in the PHP doc: http://php.net/manual/en/function.pg-query.php.

REMEMBER TO:

  • A: Escape input, or even better use prepared statements
  • B: Check the return value before iterating over the results. If an error occured it doesn't make sense to try and iterate.

In general the PHP docs are a great place, when strugling with the details of the PHP-api's, which are sometimes... Less intuitive than they could be. :-)

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.