mattc_uk Posted March 8, 2011 Share Posted March 8, 2011 Please help, when I run this search and enter something in the search thats not in the database, it doesnt reply with "Your query returned 0 results" as it should. I tjust stays blank Here's the section of code in question... if ($search) // perform search only if a string was entered. { mysql_connect($host, $user, $pass) or die ("Problem connecting to Database"); $srch="%".$search."%"; $query = "select * from tvads WHERE advert LIKE '$srch' ORDER BY advert, year DESC, details ASC LIMIT 0,30"; $result = mysql_db_query("cookuk_pn", $query); if ($result) { $count = 1; print "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#0000FF\" width=\"900\">"; print "<tr><td width=\"200\" height=\"30\" align=\"left\"><u><b><font face=\"arial\" size=\"2\" color=\"#800080\">Advert / Description</font></b></u></td>"; print "<td width=\"80\" align=\"left\"><u><b><font face=\"arial\" size=\"2\" color=\"#800080\">Year</font></b></u></td>"; print "<td width=\"200\" align=\"left\"><u><b><font face=\"arial\" size=\"2\" color=\"#800080\">Artist</font></b></u></td>"; print "<td width=\"200\" align=\"left\"><u><b><font face=\"arial\" size=\"2\" color=\"#800080\">Track</font></b></u></td>"; print "<td width=\"80\" align=\"left\"><u><b><font face=\"arial\" size=\"2\" color=\"#800080\">Buy Album</font></b></u></td>"; print "<td width=\"100\" align=\"left\"><u><b><font face=\"arial\" size=\"2\" color=\"#800080\">Download MP3</font></b></u></td></tr>"; while ($row = mysql_fetch_array($result)) { // Begin while $data .= "<td width=\"200\" height=\"30\" align=\"left\"><font face=\"arial\" size=\"1\">" .$row['advert']. "</font><br>"; $data .= "<font face=\"arial\" size=\"1\" color=\"#000080\"><i>" .$row['details']. "</i></font></td>"; $data .= "<td width=\"80\" height=\"30\" align=\"left\"><font face=\"arial\" size=\"1\">" .$row['year']. "</font></td>"; $data .= "<td width=\"200\" height=\"30\" align=\"left\"><font face=\"arial\" size=\"1\">" .$row['artist']. "</font></td>"; $data .= "<td width=\"200\" height=\"30\" align=\"left\"><font face=\"arial\" size=\"1\">" .$row['track']. "</font></td>"; $data .= "<td width=\"80\" height=\"30\" align=\"left\"><font face=\"arial\" size=\"1\">" .$row['asin_ref']. "</font></td>"; $data .= "<td width=\"100\" height=\"30\" align=\"left\"><font face=\"arial\" size=\"1\">" .$row['mp3_ref']. "</font></td></tr>"; $count++; } // end while $data .="</table>"; echo $data; } else { echo "problems...."; } } else { echo "Your query returned 0 Results"; } ?> Link to comment https://forums.phpfreaks.com/topic/230022-else-echo-not-working-at-the-end-of-a-table/ Share on other sites More sharing options...
Pikachu2000 Posted March 8, 2011 Share Posted March 8, 2011 Because the logic is if($search) { , as long as the variable $search has a value, the else{} will not be reached. You need to check how many results the query actually returns in order to determine whether to echo the message or not. Link to comment https://forums.phpfreaks.com/topic/230022-else-echo-not-working-at-the-end-of-a-table/#findComment-1184701 Share on other sites More sharing options...
taquitosensei Posted March 8, 2011 Share Posted March 8, 2011 the last else only shows if no search term was entered. Try this. $result = mysql_db_query("cookuk_pn", $query); if(mysql_num_rows($result)==0) { echo "Your query returned 0 Results"; } else if ($result) { echo "Your output." } Link to comment https://forums.phpfreaks.com/topic/230022-else-echo-not-working-at-the-end-of-a-table/#findComment-1184703 Share on other sites More sharing options...
mattc_uk Posted March 8, 2011 Author Share Posted March 8, 2011 Thanks taquitosensei that does the trick! Link to comment https://forums.phpfreaks.com/topic/230022-else-echo-not-working-at-the-end-of-a-table/#findComment-1184722 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.