I have a censored name & surname value. I want to search this value on mysql.
Censored Name = "A*** K***"
Censored Surname = "L***"
I need a mysql command to be able to run this query.
My Code:
SELECT * FROM `users` WHERE name LIKE 'A% K%' AND surname LIKE = 'L%'
ERR:
Uncaught Error: Call to a member function fetch_assoc() on bool in /opt/lampp/htdocs/x.php:15
As I mentioned above, I need to perform a search in the database in this format but as I mentioned above, I am getting an error, I would like you to provide a code sample that will work properly and smoothly.
2
Answers
This is incorrect syntax:
Just use
LIKE
as follows:You should always check for errors after preparing a query. PHP database functions like
mysqli_query()
return false if there’s an SQL syntax error like the one above.You should always check for error status returned by the database functions.
The error that you are getting is because the LIKE operator only matches the first character of the string. So, when you are trying to match the censored name "A*** K***" with the LIKE operator, it will only match the first character "A".
To fix this, you can use the % wildcard character. The % wildcard character matches any number of characters, so you can use it to match the censored name and surname.
The following code will work:
SQL
Use code with caution. Learn more
This code will match any user whose name starts with "A", has two middle letters, and ends with "K", and whose surname has one or more letters followed by "L".