Search code examples
mysqlcomparison

Trying to understand why my search is not yielding results


So in this case I am working with laravel but it might just be a MySQL "issue". I have a simple database table with only a name field that has two records:

1: B-Front

2: Frontliner

I have a string called: B-Frontliner

I am trying to get the results from the database, based on my string. In my opinion, I should get both results from my database with the following query:

$results = Artist::where('name', 'LIKE', '%' . $string . '%')->get();

But I am not getting any results. Even with using strtolower etc, no results. Also not when simply putting everything in lowercase manually, for testing purposes.

Am I misunderstanding all this, or is my query simply wrong?


Solution

  • Your condition is backwards. You want the following comparison:

    WHERE 'B-Frontliner' LIKE CONCAT('%', name, '%')
    

    You need to use the name column in the table to create the patterns, not the string the user gave.

    I'm not sure how to translate this into the ORM syntax you're using. You may need to use a raw query instead.