skip to Main Content

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


  1. This is incorrect syntax:

    surname LIKE = 'L%'
    

    Just use LIKE as follows:

    surname LIKE 'L%'
    

    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.

    Login or Signup to reply.
  2. 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

    SELECT * FROM `users` WHERE name LIKE 'A% K%' AND surname LIKE '%L%';
    

    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".

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search