Dear All,

I have a question again with my very old PHP project code(it was very dirty code) :-). It is a simple Dictionary Search program.

Currently when the Search Data not found in the mysql database, it just show the blank result. How can i add some String for those case.?

example : "Cant find your search request in our Database"

I want to add this message in my PHP code. How should I do?

Many thanks in advance.

<?

$db_name="mydb";

trim($searchterm);

if (!$_GET["searchterm"])
{
echo "You haven't entered any word to search.Please, go back and entered it.";
exit;
}
$searchterm = addslashes($searchterm);
require("dict.php");
if (!$db)
{
echo "Error. Can't connect to database.Please try later";
}
mysql_select_db($db_name);

$query = "select * from words where englishword like '".$_GET["searchterm"]."%' order by englishword";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo"<table width = '434' border='0'>";
for ($i=0; $i <$num_results; $i++)

while($data = mysql_fetch_array($result))


{     		
		echo "<tr align='left'>";
		echo "<th>";
		echo "<span class ='font'>";
		echo "{$data['englishword']}";
		echo "</span>";
		echo "</th>";
		echo "<th align='left'>";
		echo "<a href=\"/dict/audio_en/{$data['englishword']}.mp3\" target='new' class='popup-link' title=\"open the audio file for the word {$data['englishword']} \" >
		<img src='/images/audio.gif' alt = open the audio file for the word {$data['englishword']} ></a> <a href=\"http://en.wiktionary.org/wiki/{$data['englishword']}\" target='new' class='popup-link' title=\"Search this word in Free Wiki Dictionary{$data['englishword']} \" ><img src='/images/wiki.png' alt = Search this word in Free Wiki Dictionary {$data['englishword']} ></a>";
		echo "</th>";
		echo "</tr>";
		
		echo "<tr>";
		echo "<td>";
		echo "&nbsp;";
		echo "</td>";
		echo "<td  align='left'>";
		echo "<span class ='font'>";
		echo (stripslashes($data["Zword"]));
		echo "</span>";
		echo "<td>";
		echo "</tr>";


}
echo "</table>";
mysql_close($db);

?>

Recommended Answers

All 2 Replies

Your code has one superfluous loop which you did not notice because of a second error.

<?

$db_name="mydb";

trim($searchterm);

if (!$_GET["searchterm"])
{
echo "You haven't entered any word to search.Please, go back and entered it.";
exit;
}
$searchterm = addslashes($searchterm);
require("dict.php");
if (!$db)
{
echo "Error. Can't connect to database.Please try later";
}
mysql_select_db($db_name);

$query = "select * from words where englishword like '".$_GET["searchterm"]."%' order by englishword";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
if (!$num_results) die( 'No matches found.');
echo"<table width = '434' border='0'>";
/* for ($i=0; $i <$num_results; $i++) this is an unnecessary loop */

while($data = mysql_fetch_array($result))


{     		
		echo "<tr align='left'>";
		echo "<th>";
		echo "<span class ='font'>";
		echo "{$data['englishword']}";
		echo "</span>";
		echo "</th>";
		echo "<th align='left'>";
		echo "<a href=\"/dict/audio_en/{$data['englishword']}.mp3\" target='new' class='popup-link' title=\"open the audio file for the word {$data['englishword']} \" >
		<img src='/images/audio.gif' alt = open the audio file for the word {$data['englishword']} ></a> <a href=\"http://en.wiktionary.org/wiki/{$data['englishword']}\" target='new' class='popup-link' title=\"Search this word in Free Wiki Dictionary{$data['englishword']} \" ><img src='/images/wiki.png' alt = Search this word in Free Wiki Dictionary {$data['englishword']} ></a>";
		echo "</th>";
		echo "</tr>";
		
		echo "<tr>";
		echo "<td>";
		echo "&nbsp;";
		echo "</td>";
		echo "<td  align='left'>";
		echo "<span class ='font'>";
		echo (stripslashes($data["Zword"]));
		echo "</span>";
		echo "<td>";
		echo "</tr>";


}
echo "</table>";
mysql_close($db);

?>

Thanks a lot. and specialy for an unnecessary loop hint.

regards,
saya

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.