Your query is formed in a way that causes the behavior of "this-is-a-feature-not-a-bug" in MySQL: you compare the same string ('$ x') with the numeric field (id) and in the varchar field (Username).
Although I'm sure there are ways to make this work in SQL, I suspect that the only right way is to fix the PHP that creates the query. Sort of
if (is_numeric($x)) $sql="SELECT * FROM users WHERE id = '$x' OR username = '$x'"; else $sql="SELECT * FROM users WHERE username = '$x'";
should help.
Here is the SQL version, for completeness only:
SELECT * FROM users WHERE id = IF('$x' REGEXP '^[0-9]+$','$x',0) OR username = '$x'
Note: form OQ, I assume $ x is already escaped.
source share