Page 1 of 1

search Mysql help

Posted: Sat Jan 29, 2011 11:39 am
by drapet
I have this script to search mysql, works perfectly,
only need a description when nothing was found, something like "the result is not found. "
please help I have not managed to solve it.

Thanks
<form action="/result.php" method="post"><b>search:</b> <input type="text" name="term" /><input type="submit" name="submit" value="Search" />

<?php
mysql_connect ("localhost", "user","pass") or die (mysql_error());
mysql_select_db ("db_name");


$term = $_POST['term'];
$sql = mysql_query("select * from radio where beschrijving like '%$term%' ORDER by titel");
while ($row = mysql_fetch_array($sql))

{
echo '<div id="rij">';
echo "<br/><a href='http://domain.com/sender/{$row['id']}/{$row['titel']}.html'><img src='{$row['img']}' title= 'Radio {$row['titel']} {$row['grad']}' border=0 ALIGN=LEFT></a>";
echo '<div id="result1">';
echo "<a href='http://domain.com/sender/{$row['id']}/{$row['titel']}.html'><b>Radio {$row['titel']}</b></a>";
echo '<br/><b>'.$row['grad'];echo '</b>';
echo '<br/>'.$row['beschrijving'];
echo '<br/>';
echo '</div>';
echo '</div>';
}
?>

Re: search Mysql help

Posted: Sat Jan 29, 2011 11:58 am
by social_experiment
Using mysql_num_rows() check how many rows are returned by the query and according to that result you respond.

Code: Select all

<?php
$sql = mysql_query("select * from radio where beschrijving like '%$term%' ORDER by titel");
$rows = mysql_num_rows($sql);
if ($rows == 1) {
 // code that displays results
}
else {
 // no rows matchin query
 echo 'the result is not found';
}
?>
Hth

Re: search Mysql help

Posted: Sun Jan 30, 2011 6:30 am
by drapet
Not working

When I insert the cod will not find anything constantly shows no results.

Re: search Mysql help

Posted: Sun Jan 30, 2011 10:09 am
by social_experiment
Paste the code that you created (including the modifications).

Re: search Mysql help

Posted: Sun Jan 30, 2011 10:11 am
by drapet
I solved it this way.
now works perfectly.

thanks anyway.

Code: Select all

<?php
mysql_connect ("localhost", "user","pass") or die (mysql_error());
mysql_select_db ("db_name");

$term = $_POST['term'];

 $sql = "
      SELECT 
          * 
      FROM 
          radio 
      where beschrijving like '%$term%' 
      ORDER by titel   
      ";  
      
      if(!$res = mysql_query($sql))
      {
         trigger_error(mysql_error().'<br />In query: '.$sql);
      }
      elseif(mysql_num_rows($res) == 0)
      {
         echo 'No channels found';
      }
      else
      {  
               
     while ($row =mysql_fetch_array($res))  
      { 
{
echo '<div id="rij">';
        echo "<br/><a href='http://radio.sveizfotelje.com/sender/{$row['id']}/{$row['titel']}.html'><img src='{$row['img']}' title= 'Radio {$row['titel']} {$row['grad']}' border=0 ALIGN=LEFT></a>";
 echo '<div id="result1">';
        echo "<a href='http://radio.sveizfotelje.com/sender/{$row['id']}/{$row['titel']}.html'><b>Radio {$row['titel']}</b></a>";
        echo '<br/><b>'.$row['grad'];echo '</b>';
        echo '<br/>'.$row['beschrijving'];
	echo '<br/>';
 echo '</div>';
echo '</div>';
	}
    }
}	
?>