0

Hiiii,Good Evening
I have a code that search for data in the database but I can search for only 1 keyword in a time,which is working fine for single keyword. if I type 2 keywords separated by a space or comma's which return blank page. I would like to modify it so I can search for multiple keywords which is separeted by commas.
Here is my code

<?php
mysql_connect("localhost" , "root" , "") or die(mysql_error());
mysql_select_db("keyword") or die(mysql_error());
if($_POST['submit'])
{
$keyword=$_POST['keyword'];
$keyword = mysql_real_escape_string( trim($_POST['keyword']));
$sql="SELECT title FROM search WHERE keyword LIKE '%$keyword%'";
 $query=mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{

    $title =$row['title'];
    echo "$title<br/>";
}
}


?>

please help me.

3
Contributors
7
Replies
34
Views
1 Year
Discussion Span
Last Post by pritaeas
2
$keyword = mysql_real_escape_string( trim($_POST['keyword']));
$keywords = split(' ', $keyword);
foreach ($word in $keywords)
    $queryKeyword[] = "'%$word%'";

$where = implode(' OR keyword LIKE ', $queryKeyword);
$sql = "SELECT title FROM search WHERE keyword LIKE $where";
0

but stil my problem is not solved,
this is my data in database(see this attachment) Screenshot.jpg .

if i enter single keyword it works fine ,if i enter "java,php" as a search keyword it wil show only "php courses" as a result. i want to modify this code such as if i enter "php,java" as a search key word ,it will show all data in database which has either 'php' or 'java' keyword stored in database .
this is my code

<?php
mysql_connect("localhost" , "root" , "") or die(mysql_error());
mysql_select_db("keyword") or die(mysql_error());
if($_POST['submit'])
{
$keyword=$_POST['keyword'];
$keyword = mysql_real_escape_string( trim($_POST['keyword']));
$keywords = split(' ', $keyword);
foreach ($keywords as $word)
    $queryKeyword[] = "'%$word%'";
$where = implode(' OR keyword LIKE ', $queryKeyword);
$sql = "SELECT title FROM search WHERE keyword LIKE $where";
 $query=mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{

    $title =$row['title'];
    echo "$title<br/>";
}
}

?>

Any help??

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.