The following is in my sql's where statement. It's exploding the words to individual letters instead of splitting phrases. How do I fix that? if (!empty($keyword2)) { $myarray = explode(' ', $keyword2); $i = 0; $whereclause .= " AND text_data LIKE '%".$value[0]."%'"; $i++; foreach($myarray as $value) { $whereclause .= " OR text_data LIKE '%".$value[$i]."%'"; $i++; } $sql .= $whereclause; $i++; } PHP:
if (!empty($keyword2)) { $myarray = explode(' ', $keyword2); $i = 0; $whereclause .= " AND text_data LIKE '%".$value[0]."%'"; $i++; foreach($myarray as $value) { //$whereclause .= " OR text_data LIKE '%".$value[$i]."%'"; error is here. $whereclause .= " OR text_data LIKE '%".$value."%'"; $i++; } $sql .= $whereclause; $i++; } PHP: Try this. Its working now.
No it didn't work. It's starting with OR instead of AND. The first has to start with AND. I have: if (!empty($keyword1)) { $myarray = explode(' ', $keyword1); foreach($myarray as $value) { $whereclause .= " AND text_data LIKE '%".$value."%'"; } $sql .= $whereclause; $i++; } if (!empty($keyword2)) { $myarray = explode(' ', $keyword2); $whereclause .= " AND text_data LIKE '%".$value[0]."%'"; foreach($myarray as $value) { $whereclause .= " OR text_data LIKE '%{$value}%'"; } $sql .= $whereclause; } PHP: My query is supposed to start like this: But it's starting like this: So the search is displaying the entire book.
After executing above code I got this In the line $whereclause .= " AND text_data LIKE '%".$value[0]."%'"; $value[0] is undefined so I am not getting value in sql statement
use this code $keyword2 = "how are you"; if (!empty($keyword2)) { $myarray = explode(' ', $keyword2); $i = 0; $i++; $a=0; foreach($myarray as $value) { if($a==0){ $whereclause .= " AND text_data LIKE '%".$value[0]."%'"; }else{ $whereclause .= " OR text_data LIKE '%".$value."%'"; } $a++; } $sql .= $whereclause; $i++; } echo $sql; PHP:
change one thing which is wrongly entered, try this one $keyword2 = "how are you"; if (!empty($keyword2)) { $myarray = explode(' ', $keyword2); $i = 0; $i++; $a=0; foreach($myarray as $value) { if($a==0){ $whereclause .= " AND text_data LIKE '%".$value."%'"; }else{ $whereclause .= " OR text_data LIKE '%".$value."%'"; } $a++; } $sql .= $whereclause; $i++; } echo $sql; PHP: