ok; this has been frying my brain for hours. I think I might need a sub query, but I'm not that advanced at this kind of thing so any help or pointers in the right direction would be greatly appreciated.
Here's the Query I have....
$query = "SELECT * FROM events WHERE event_type = 'Christmas Listings' AND event_active='Yes' ORDER BY event_date ASC LIMIT 5";
$result= mysql_query($query);
OK... now for the plain english bit on what I want to achieve (to understand what I'm trying to achieve):
I want to check the event type ('
event_type
') is what I'm getting (ie. Christmas Listings) as there are multiple types in this column.I want to check the event is active ('
event_active
') is Yes(the data in this field is Yes/No).I want to order them by the ('
event_date
') ASC (the data in this field isyyyy-mm-dd
) so they show the latest entry by its date from the DB.I want to LIMIT (or in some way control the output) the results to only have 5 results displayed when running this kind of query through a WHILE statement.
OK, this all works BUT; when I get to the actual output display, i'm having a shaky output in how many results are actually display... What happens is if I have multiple events which are switched off, as in event_active
is 'Off
' then its almost like the argument is counting from the all the results that are (including event_active='Off'
) and consequently not showing how I expect them to display?
Hope this makes sense.... Any help would be gratefully received.
That query should be fine. I'd check your data set (or even better, post it!). You might want to look into normalizing the database too. It'll help you out in the future.
I am really not sure what you are asking, but
LIMIT
works as follows:The
LIMIT
means that after your query is done, and ALLWHERE
statements are processed, only the first 5 are returned.ALl results where
event_active
is not 'Yes' will not be shown, and disregarded in everything.This result is the same as a result where you would do the query without the limit, and just look at the first 5 lines.
so your statement is easyer to read..
The problem, I think, is with your 'event_active'.
MySQL uses 0 and 1 to indicate whether the field is true/false, yes/no, on/off. Try using 0 and 1 in your SELECT statement unless the field type on that field is VARCHAR and you actually are using those words.