MySQL Security – How to Prevent SLEEP Injection

MySQL

I was wondering if someone could help to understand this query, basically I'm aware about SLEEP() function but I've never seen this implementation before.
Why there are no any results returned?

mysql> SELECT fname, lname FROM contacts WHERE fname = 'test' / SLEEP(1) / '';
Empty set, 14 warnings (7.00 sec)

Warnings:

Warning | 1292 | Truncated incorrect DOUBLE value: 'test'
...

Best Answer

No results are returned because, unsurprisingly, there are no records that meet the conditions of the WHERE clause. SLEEP() will return 0 (if not interrupted), and all operands will be cast as doubles for the division operation (hence the warning about truncated incorrect double). In essence, the query is the same as

SELECT fname, lname FROM contacts WHERE fname = 0 / 0 / 0;

Division by zero results in null, so you're basically looking for records where fname = NULL, which is never true.