Let's say we have the following code (for some kind of search or the like):
$stmt = $pdo->prepare("SELECT * FROM users WHERE username LIKE ?"); $stmt->execute(array('%' . $username . '%'));
The username you entered is correctly escaped, but % (= 0 or more arbitrary characters) and _ (= exactly 1 arbitrary characters) are interpreted as a MySQL wildcard.
I understand that users can enter % or _ to search, and I should avoid it if I want the search function to work correctly. (In cases like set_pt and getting setopt as a result).
But my question is: could anyone use this? If so, how can someone use this and how to prevent it? Could the function be lower?
function escape_like_string($str) { return str_replace(Array('%', '_'), Array('\%', '\_'), $str); }
One of the possibilities that I could think of is to enter tons of % , so the server will need to allocate a lot of memory. Will this work?
source share