mySQL no result to show different code
Posted: Wed Jul 29, 2009 8:21 pm
Hi all,
I have a MySQL query that works well, unless there is no result found and then the PHP code doesn't render correctly. The code between the -- while ($row = mysql_fetch_array($result)) { ... } (lines 10 - 23) -- doesn't render and the closing div then is missing and ruins the layout of the page. Can I create an if() that will show a generic table when the MySQL query returns no results? How would this be done?
Thank in advance!
I have a MySQL query that works well, unless there is no result found and then the PHP code doesn't render correctly. The code between the -- while ($row = mysql_fetch_array($result)) { ... } (lines 10 - 23) -- doesn't render and the closing div then is missing and ruins the layout of the page. Can I create an if() that will show a generic table when the MySQL query returns no results? How would this be done?
Code: Select all
$result = mysql_query("SELECT * FROM `table` WHERE `1` LIKE '$search' OR `2` LIKE '$search' OR `3` LIKE '$search' LIMIT 0, 1");
echo "<p>Your search for <strong>".$search."</strong> returned the following results:</p>";
echo '<div id="table">
<table width="400" border="0">';
while($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<th>1</th>';
echo '<th>2</th>';
echo '<th>3</th>';
echo '</tr>';
echo '<tr align="center">';
echo '<td>' . $row['1'] . '</td>';
echo '<td>' . $row['2'] . '</td>';
echo '<td>' . $row['3'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="3" align="center"><img src="resources/' . $row['4'] . '" width="200" height="200" alt="image" /></td>';
echo '</tr>';
echo "</table></div><p> </p>";
}