search Mysql help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drapet
Forum Newbie
Posts: 3
Joined: Sat Jan 29, 2011 11:18 am

search Mysql help

Post 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>';
}
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: search Mysql help

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
drapet
Forum Newbie
Posts: 3
Joined: Sat Jan 29, 2011 11:18 am

Re: search Mysql help

Post by drapet »

Not working

When I insert the cod will not find anything constantly shows no results.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: search Mysql help

Post by social_experiment »

Paste the code that you created (including the modifications).
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
drapet
Forum Newbie
Posts: 3
Joined: Sat Jan 29, 2011 11:18 am

Re: search Mysql help

Post 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>';
	}
    }
}	
?> 
Post Reply