Search code examples
phpmysqlsql-like

How to search a pattern in mysql table value using keyword "LIKE"


I tried with this query,

$str =$_POST['val'].'%';
$query = mysql_query("select * from table WHERE title LIKE '$str' ORDER BY createdtime DESC LIMIT 0, 70 ");

but it searches the match only in the start of the word. Not search else where in the world. For an example when I search for pattern 'gold' in the word of 'golden', it detect correctly. But not in the middle or end or elsewhere, words like 'a gold' or 'a golden' it will not display them correctly.


Solution

  • $query = mysql_query("select * from table WHERE title LIKE '%$str%' ORDER BY createdtime DESC LIMIT 0, 70 ");