Forum Moderators: coopster

Message Too Old, No Replies

PHP Postgresql Error

PHP Postgresql Error

         

Imy_S3

2:42 pm on Mar 15, 2004 (gmt 0)

10+ Year Member



Hi

I have the following:

$sql= "select shopname from shop where exits SELECT shopname FROM shop where shoptype = '$_POST[option]'";

Its embedded within php.

keep getting error:
sportsselect shopname from shop where exits SELECT shopname FROM shop where shoptype = 'sports'
Warning: pg_exec(): Query failed: ERROR: parser: parse error at or near "SELECT" at character 39 . in /home/students/ug/ug75ixc/public_html/shops/storeSearch.php on line 46

i know what i am doing, but i dont think i got syntax right, can anyone help

coopster

5:25 pm on Mar 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Subqueries should be parenthesized and you have the
EXISTS
keyword spelled incorrectly:

$sql= "select shopname from shop where EXISTS (SELECT shopname FROM shop where shoptype = '$_POST[option]')";

But what is it exactly that you are trying to accomplish? I'm not so sure you need an

EXISTS
predicate here, nor a subquery. You may be getting a result set that you aren't expecting.

Imy_S3

6:23 pm on Mar 15, 2004 (gmt 0)

10+ Year Member



Hi

basically what i am trying to do is select the shop names from the table based on the users choice.

But i want the results to come out alphabetically

For example:

if the user wants to dipsly results of clothes shop this query would be used:

SELECT * FROM shop where shoptype = 'clothes'

in php scipt its
SELECT * FROM shop where shoptype = '$POST[option]';

so how would i do it if i wanted these results to be displayed alphabetically

coopster

6:26 pm on Mar 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are on the right track, but you want an
ORDER BY
clause:

SELECT * FROM shop where shoptype = '$POST[option]' ORDER BY shoptype;

The default is ascending order (ASC), but if you want it in descending order:

SELECT * FROM shop where shoptype = '$POST[option]' ORDER BY shoptype DESC;

coopster

6:44 pm on Mar 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



One last thing. You should always use quotes around a string literal array index. If you don't, PHP will throw
NOTICE
errors. There are a number of ways to overcome this issue, but here is one correct way:

$sql = "SELECT * FROM shop where shoptype = '" . $POST['option'] . "' ORDER BY shoptype;

See the PHP Array do's and don'ts [php.net] for more information regarding this practice.

Imy_S3

7:42 pm on Mar 15, 2004 (gmt 0)

10+ Year Member



Cheers mate.

I understand what is going on, but just mixed up with the sytax as i am new to PHP.

Thanks alot, that worked well.