I have a query in my application that selects users from a table by ID or by username:
SELECT * FROM users WHERE id = '$x' OR username = '$x'
This is working when given usernames like foo, bar123 or ids like 1, 123.
But when I give a username like 2foo it selects both user 2foo and user with id=2. So it takes the 2 of 2foo and finds a user. Additionally I get a warning message: 1292: Truncated incorrect DOUBLE value: 2foo.
Is there a way to tell MySQL not to do this conversion (for this query but not whole db)? Or do I need to do a filtering after the query to discard false results?
SELECT * FROM users WHERE id = '2foo' OR username = '2foo'. There is no surrounding code as I identified this query as the problematic one from my application. MySQL-Version: 5.1.41-3ubuntu12.10if(is_numeric($x)){ $sql = "SELECT * FROM users WHERE id = $x OR username = '$x'" } else { $safeX = mysql_real_escape_string($x); // Or prepared statements, or whatever else you prefer. $sql = "SELECT * FROM users WHERE username = '$x'" }