I want to explain basic searching techniques using SQL LIKE statement.
Replace spaces
Before executing SQL query searching word replace spaces with %(symbol) using PHP str_replace function.
Here searching word jquery ajax spaces replaced with %(symbol)- jquery%ajax
SQL ‘LIKE’
SELECT * FROM messages WHERE msg LIKE ‘%jquery%ajax%’ ;
CODE:
<?php
if($_SERVER[“REQUEST_METHOD”] == “POST”)
{
$q=$_POST[‘q’];
$q=mysql_escape_string($q);
$q_fix=str_replace(” “,”%”,$q); // Space replacing with %
$sql=mysql_query(“SELECT title FROM articles WHERE title LIKE ‘%$q_fix%'”);
}
?>
search unicode or UTF-8 data
SQL ‘LIKE N’
SELECT * FROM articles WHERE title LIKE N’%search_word%’ ;
Why prefix ‘N’?
Unicode – The N stands for National language character set. Which means that you are passing an NCHAR, NVARCHAR or NTEXT value