Forum Moderators: coopster

Message Too Old, No Replies

Problems with WHERE in query

im wondering how to make query look for multiple things...

         

GamingLoft

8:13 pm on Jul 31, 2007 (gmt 0)

10+ Year Member



ok

Im wondering how i can make the query look for rows WHERE fid = 79 or 80 or 81 or 82

is it like this?

mysql_query("SELECT * FROM ".TABLE_PREFIX."threads
WHERE fid='79, 80, 81, 82' ORDER BY `tid` DESC LIMIT 0 , 50") or die(mysql_error());

Gibble

8:18 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In this case, I'd use 'IN'

SELECT * FROM ".TABLE_PREFIX."threads
WHERE fid IN (79, 80, 81, 82) ORDER BY `tid` DESC LIMIT 0 , 50

bcolflesh

8:18 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use "IN":

[webmasterworld.com...]

*edit* - Gibble beat me to it.

GamingLoft

8:27 pm on Jul 31, 2007 (gmt 0)

10+ Year Member



i tried using WHERE IN but i'm getting this error on my page now:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/anicedomain/public_html/sumpage.php on line 20

------------

this is the line:

while($r=mysql_fetch_array($result))

------------

if you need to know what $result is:

$result = mysql_query("SELECT * FROM ".TABLE_PREFIX."threads WHERE subject LIKE '%$search%' WHERE fid IN (79, 80, 81, 82) ORDER BY `tid` DESC LIMIT 0 , 100000");

Gibble

8:28 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and if your 79,80... list is long and generated with a sql subquery, use EXISTS instead of IN, with the proper syntax changes.

Gibble

8:29 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I notice you're using fid, then `tid`...these are different columns right?

Second, is fid (if that's the correct name) of an integer type, or is it a string? if it's a string, use '79', '80', etc

GamingLoft

8:32 pm on Jul 31, 2007 (gmt 0)

10+ Year Member



yes, fid is like the category its in and tid is its id.

im tryna make the query show only stuff with the certain fid's'.

Fid is a int.

[edited by: GamingLoft at 8:33 pm (utc) on July 31, 2007]

GamingLoft

8:34 pm on Jul 31, 2007 (gmt 0)

10+ Year Member



I googled for php mysql EXIST/EXISTS but i couldn't find anything can anyone recommend a tutorial or show an example?

Gibble

8:48 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[mysql.com...]

Here's the reference for it.

bcolflesh

8:53 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"WHERE subject LIKE '%$search%' WHERE fid IN (79, 80, 81, 82)..."

Missing AND?

Gibble

9:25 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, geeze, ya, there it is, you have two WHERE clauses, change the second one to read AND

SELECT * FROM ".TABLE_PREFIX."threads WHERE subject LIKE '%$search%' AND fid IN (79, 80, 81, 82) ORDER BY `tid` DESC LIMIT 0 , 100000