1

This won't work and i'm getting an error from the ORDER BY param for some reason?

if($selected_radio == 'city')
{
    $query = "SELECT name FROM City WHERE name LIKE $1 LIMIT $2 OFFSET $3 ORDER BY name ASC";
    $result = pg_prepare($conn, "findCity", $query);
    $result = pg_execute($conn, "findCity", array($text, $limit, $offset));

    while($row = pg_fetch_assoc($result))
    {
        echo "<tr>";
        echo "<td>" . $i . "</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "</tr>";
        $i += 1;
    }
}

Sorry about the way it looks I'm fairly new to this site and not sure how to correctly post code :/

4
  • Can you include the exact error message? Commented Sep 10, 2012 at 22:29
  • To format code, indent it by 4 spaces or select it and click the {} button. Commented Sep 10, 2012 at 22:31
  • I'm not receiving an error message for some reason? Commented Sep 10, 2012 at 22:33
  • @JoshValdivieso You should be, so you need to investigate that too. It's really important that statements that fail report error messages so you can act on them. Commented Sep 10, 2012 at 22:48

1 Answer 1

4

You've got your clauses in the wrong order - ORDER BY goes before LIMIT and OFFSET.

From the docs:

SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
     * | expression [ AS output_name ] [, ...]
     [ FROM from_item [, ...] ]
     [ WHERE condition ]
     [ GROUP BY expression [, ...] ]
     [ HAVING condition [, ...] ]
     [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
     [ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]
     [ LIMIT { count | ALL } ]
     [ OFFSET start ]
     [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]
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.