-1

So I'm trying to pick 2 rows randomly but the id isn't allowed to be equal to x or equal to y.

That's what I tried so far:

  $statment = $db -> "SELECT * FROM player ORDER BY RAND() LIMIT 2 WHERE 
              NOT(id = ?) OR WHERE NOT(id=?)";
  $statement->execute(array($player1, $player2));

But getting:

Parse error: syntax error, unexpected '"SELECT * FROM player ORDER BY' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE)

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Ibrahim
  • 85
  • 8

2 Answers2

5

You need AND to make sure it is not equal <> to both:

WHERE id <> ? AND id <> ?    

Or check if it is not IN a list:

WHERE id NOT IN(?, ?)

And I'm not sure about the order of the SQL statement, try:

SELECT * FROM player WHERE id <> ? AND id <> ?
         ORDER BY RAND() LIMIT 2 

Also, you're missing something like maybe prepare:

$statement = $db->prepare("SELECT * FROM player WHERE id <> ? AND id <> ?
                                   ORDER BY RAND() LIMIT 2");
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
-1

Use NOT IN. You're also missing the call to prepare().

$statment = $db ->prepare("SELECT * FROM player
                    WHERE id NOT IN (?, ?)
                    ORDER BY RAND()
                    LIMIT 2");

Also, the order of clauses in a SELECT statement is:

SELECT
FROM
JOIN
WHERE
GROUP BY
HAVING
ORDER BY
LIMIT
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Parse error: syntax error, unexpected '"SELECT * FROM player ' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in – Ibrahim Oct 13 '17 at 18:41
  • Not my dv here, but [you missed this..........](https://stackoverflow.com/questions/46736093/php-mysql-select-where-id-not-equal-to-two-one-of-2-ids#comment80419617_46736093). – Funk Forty Niner Oct 13 '17 at 19:01