PHP Search result help?

Kelly H

New member
Here is the code i have


<?php
mysql_connect ("localhost", "***********","**********") or die (mysql_error());
mysql_select_db ("kelly");

$term = $_POST['term'];

$sql = mysql_query("select * from text where Color like '%$term%' or ItemNumber like '%$term%' or Discription like '%$term%'or Price like '%$term%' or Information like '%$term%' ");


while ($row = mysql_fetch_array($sql)){
echo ''.$row['URL'];
echo '<br/> Discription:&nbsp '.$row['Discription'];
echo '<br/> ItemNumber:&nbsp '.$row['ItemNumber'];
echo '<br/> Price:&nbsp$ '.$row['Price'];
echo '<br/><br/>';
}

?>



Here is what i need it to do.
Say you do a search for oreo cookie this is going to search my DB well the only problem is that i dont have any thing close to oreo cookie in there. Right now if i were to do a search for it it would just give me a blank result. What code would i need to add to my code to have it say " The search you just made did not find any thing Please try again"
 
Try this:
<?php
mysql_connect ("localhost", "***********","**********") or die (mysql_error());
mysql_select_db ("kelly");
$term = $_POST['term'];
$sql = mysql_query("select * from text where Color like '%$term%' or ItemNumber like '%$term%' or Discription like '%$term%'or Price like '%$term%' or Information like '%$term%' ");
if(!mysql_fetch_array($sql)){
echo "Your search did not match with anything. Please try again";
}else{
while(list($url,$description,$itemNumber,$price)=mysql_fetch_array($sql)){
echo ''.$url;
echo '<br/> Discription:&nbsp '.$description;
echo '<br/> ItemNumber:&nbsp '.$itemNumber;
echo '<br/> Price:&nbsp$ '.$price;
echo '<br/><br/>';
}
}
?>
I cannot guarantee if your code is correct.
If it doesn't Create another query, except where it says "SELECT * FROM", do "SELECT COUNT(*) FROM".
$sql = SELECT COUNT(*) FROM...
$result = mysql_fetch_array($sql);
$result = $result[0];
if($result != 0){ ... display results... }else{...no matches...}
Good Luck!
If you need further assistance/advice, feel free to contact me.
 
Back
Top