I have a mysql table of user like below:
id name phone ---------------------------- 1 John +1-32-5435-2343 2 Tom +92-32-43434-233 3 Eve +5-323-43-23234
$phoneNumber = '+13254352343';
How can I do where query to return the user that match this phone number by ignoring dashes
What I know is:
SELECT * FROM users WHERE phone = $phoneNumber
Answer
One method is to replace the dashes for the comparison:
where replace(phone, '-', '') = ?
The ?
is a parameter placeholder for the comparison value. Don’t munge the query string with a literal value.
Note: This prevents the use of indexes.