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.

Recommended Answers

All 7 Replies

$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";

thanks for your code

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??

Any Help #Pritaeas??

sorry...and thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.