What is wrong with this query:
SELECT * FROM leaderboard WHERE ALREADYWON != '1' ORDER BY Score LIMIT 0,'$limit'
Code after it just throws errors:
Warning: Invalid argument supplied for foreach() in file ...
$limit
definitely has a value, I'm echoing it out just before. I'm using Oracle.
Can anyone spot a syntax error?
ORACLE doesn't support LIMIT. I had to use this query
SELECT * FROM leaderboard
WHERE rownum <= '$limit'
AND ALREADYWON != '1'
ORDER BY Score
Is ALREADYWON really a string? Shouldn't the comparison be using <> instead of != ?
Mixing PHP into a SQL statement is the problem. Everything after this...
SELECT * FROM leaderboard WHERE ALREADYWON != '1' ORDER BY Score
... will likely be the cause of your error
Also, its considered poor practice to 'SELECT *' for any table. You should be naming the columns explicitly.
After looking at some of your responses, it looks like you are trying to get TOP N records. For oracle, this is a few of the ways to get them (kind of dated, but should still work).
I'd say the != is wrong and should be <> to be a valid query. Also I don't know but '$limit' should perhaps be '".$limit."'. GL!
Oracle does not support the LIMIT keyword. The equivalent construct in Oracle would be something like
SELECT *
FROM (SELECT *
FROM leaderboard
WHERE AlreadyWon != '1'
ORDER BY score)
WHERE rownum <= <<limit>>
Note that this is NOT equivalent to the query
SELECT *
FROM leaderboard
WHERE AlreadyWon != '1'
AND rownum <= <<limit>>
ORDER BY score
The first query gets the first <<limit>>
rows based on the SCORE
order. The second query gets an arbitrary set of <<limit>>
rows and orders that arbitrary set because the ROWNUM filter is applied before the ORDER BY clause.
SELECT * FROM
(SELECT score, ROW_NUMBER() OVER (ORDER BY score) R FROM leaderboard WHERE alreadywon != 1)
WHERE R BETWEEN 0 and '$Limit';
This will get you scores ordered but up to the limit you specified. Be careful though, as this is a candidate for SQL Injection. You might want to look into PHP parameterized queries instead.