Log in

View Full Version : Something REALLY simple just isn't working



MrRSMan
12-18-2009, 07:01 PM
I'm using the following code with no problems:


$query="SELECT * FROM {$table} WHERE id = '1' or id % 24 = 0 LIMIT 0, 90";

But as soon as I try to order it, it stops working- no data comes back out. This is the code I'm using, and so far as I can see, it's faultless:


$query="SELECT * FROM {$table} WHERE id = '1' or id % 24 = 0 LIMIT 0, 90 ORDER BY created";

The field "created", before you ask, does definitely exist and does contain data. The field contains timestamps (e.g. 2009-12-17 16:00:12), but I've tried ordering by other INT fields with the same outcome.

Thanks, mrrsman

djr33
12-18-2009, 08:39 PM
First, you can start by making the mysql in the right format.
1. A proper mysql statement ends with a semicolon. (This can also possibly help with security issues by ending a query to not allow injection in some cases.)
2. Names (columns, tables, etc) need `` quotes-- "backticks", above the tab key. They aren't really required, but it will make it more valid.
3. Values need quotes ' ' or " "-- since you're using double quotes for the outside here, use single quotes ' ' for the data. This won't matter for numbers in most cases, though.
4. You can specify DESC or ASC (descending, ascending) after ORDER BY. I don't think it's required, but maybe that would help?

Anyway, looking at the code the only thing I can think of is the order of it. I think order by and all other specifics must go before the limit. Limit is supposed to end the query, so I think the parser may be confused by "order by" after that.

bweinman
12-19-2009, 03:54 PM
ORDER BY goes before LIMIT. Try this:

$query="SELECT * FROM {$table} WHERE id = '1' or id % 24 = 0 ORDER BY created LIMIT 0, 90";