N.N.Ontika N.N.Ontika - 2 years ago 66
SQL Question

MySQL search for multiple keywords in same column

I am facing some problems in MySQL query in PHP.

I have the below Bengali words in a column named 'keywords' and they are assigned to 'গৌরিপ্রসন্ন মজুমদার' value of another column named 'name' in a table named 'lyricist'


When I'm searching for some consecutive words like 'তোমার,আমার,তুমি' I get the correct answer 'গৌরিপ্রসন্ন মজুমদার' But when i'm searching for random words like 'তোমার,তুমি' it's not giving any answer.

$query = "SELECT name FROM `lyricist` WHERE keywords LIKE '%$val%'";

Here $val has 'তোমার,আমার,তুমি' or 'তোমার,তুমি' these type of values.

Why not it's treating the item randomly? Please HELP.

Answer Source
$query = "SELECT name FROM `lyricist` WHERE keywords LIKE '%$val%'";

Will get you all the matching columns that start with some text having your value and then end with some text.So if you put some random words and they are not in the correct order obviously this query will not match.

I think what you want to do is when you have multiple values is put them in different variable val1,val2,... and using AND in your query

$query = "SELECT name FROM `lyricist` WHERE keywords LIKE '%$val1%' AND keywords LIKE '%$val2%'";

So you would have to construct your query dynamically something like this would work:

     $query="SELECT name FROM `lyricist` WHERE keywords LIKE '%$keywords[0]%' ";  
         $query.="AND keywords LIKE '%$keywords[$i]%' ";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download