Forum Moderators: coopster

Message Too Old, No Replies

mysql "select from" question

         

pixel_juice

1:54 am on Jul 20, 2003 (gmt 0)

10+ Year Member



I'm rattling along nicely with my php/mysql experimenting. However I have hit upon another problem that I don't quite understand.

I want to display widgets from the same category as links underneath the particular widget the visitor is viewing.

I'm using something similar this code:

SELECT * FROM thetable WHERE widget!='$widget' AND category='$category' LIMIT 4

do {

printf("<p><a href=\"%s?widget=%s\">%s</a><br>-$s\n", $PHP_SELF, $myrow["widget"], $myrow["title"]);

printf("%s\n", $myrow["description"]);

} while ($myrow = mysql_fetch_array($result));

This does just want I want it to (displays 4 widgets from the same category), but the current widget is always displayed at the top of the list, despite my efforts to remove it (with WHERE widget!='$widget') and making 5 total entries rather than 4.

Can anyone see anything wrong with the code above that might cause this?

WebJoe

2:26 am on Jul 20, 2003 (gmt 0)

10+ Year Member



Pixel_juice: I know the expresion
!=

to negate
=

but from AFAIK in SQL only
<>

works.

bonanza

3:51 am on Jul 20, 2003 (gmt 0)



Pixel_juice,

The way you have that written in your question, if the query produces four results, you're going to execute the loop code five times. The while condition runs after the first time through the loop.

Try calling

$myrow = mysql_fetch_array($result)

before the 'do' line as well to start it off. Otherwise, you're getting the $myrow variable that was previously set. It seems to have been previously set given that you're actually getting values on the first time through the loop.

hakre

9:25 am on Jul 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



making 5 total entries rather than 4.

this is controlled by LIMIT. change it from LIMIT 4 to LIMIT 5 and you're done for this.

-hakre

pixel_juice

11:56 pm on Jul 20, 2003 (gmt 0)

10+ Year Member



>>Try calling $myrow = mysql_fetch_array($result) before the 'do' line as well to start it off.

Thanks a million WebJoe, I did this and it now works perfectly :)

bonanza

4:57 pm on Jul 21, 2003 (gmt 0)



Hey! That wasn't webjoe!

;) glad it worked, I didn't do a great job explaining.

pixel_juice

9:44 pm on Jul 21, 2003 (gmt 0)

10+ Year Member



So sorry bonanza :(

Maybe if I learn to read properly, I wouldn't be posting my newbie sql questions so often ;)

So thanks to you bonanza (and also to webjoe - he was right about the negate command) :)

WebJoe

12:15 am on Aug 10, 2003 (gmt 0)

10+ Year Member



Glad I could help a bit, even though you gave me too much credit...;)