light-angel Posted November 8, 2010 Share Posted November 8, 2010 this is my code but i want to have a pice so if their is no results the it will put in a message like "No Results" but i cant figger it out <?php $host="localhost"; $username="xxxxx"; $password="xxxxxx"; $db_name="xxxxxxx"; $tbl_name="Shows"; // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name WHERE Month='April'and Year='2011' order by Day asc "; $result=mysql_query($sql); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="400" border="1" cellspacing="0" cellpadding="3"> <?php while($rows=mysql_fetch_array($result)){ ?> <span class="elevenbold"><? echo $rows['Day']; ?> <? echo $rows['Month']; ?> <br><strong class="elevenboldblue"><? echo $rows['Song']; ?></strong><br><? echo $rows['Data']; ?></span><br><br> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted November 8, 2010 Share Posted November 8, 2010 Put your code in [php [/php tags for others. You're looking for mysql_num_rows(); $result=mysql_query($sql); if(mysql_num_rows($result) == 0){ echo 'No Results Found.'; return; } Quote Link to comment Share on other sites More sharing options...
ninedoors Posted November 8, 2010 Share Posted November 8, 2010 Try this: <?php $host="localhost"; $username="xxxxx"; $password="xxxxxx"; $db_name="xxxxxxx"; $tbl_name="Shows"; // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name WHERE Month='April'and Year='2011' order by Day asc "; $result=mysql_query($sql); echo ' <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="400" border="1" cellspacing="0" cellpadding="3">'; if (mysql_num_rows($result) > 0) while($rows=mysql_fetch_array($result)) echo '<tr><td><span class="elevenbold">'.$rows['Day'].' '.$rows['Month'].'<br /><strong class="elevenboldblue">'.$rows['Song'].'</strong><br />'.$rows['Data'].'</span><br /><br /></td></tr>'; else echo '<tr><td>No Results</td></tr>'; echo ' </table> </td> </tr> </table>'; mysql_close(); ?> You should really look at your html structure as well as it is not laid out well and you will run into problem when you are trying to do more complex pages. Quote Link to comment Share on other sites More sharing options...
light-angel Posted November 8, 2010 Author Share Posted November 8, 2010 thank you for the help all working well now as for the html structure that is all sorted in the main page and the php&mysql page is just called in by the main page Quote Link to comment Share on other sites More sharing options...
radar Posted November 8, 2010 Share Posted November 8, 2010 I personally find that mysql_num_rows can be quite slow sometimes, especially when you are fetching hundreds of rows at a time (which I do frequently).. So option, adds a few lines of code. First off I add SQL_CALC_FOUND_ROWS into my query so: <?php $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM table"; ?> Then to retrieve that I add a minimum of 3 lines of code. $_query = "SELECT FOUND_ROWS() as total"; $_result = mysql_query($_query); $_row = mysql_fetch_array($_result, MYSQL_ASSOC); I tend to do my select statement outside of mysql_query just for my own ease of reading later. With this code I would then access count by using: $_row['total'] With that you'd then use the same type of if statement as in anti-moronic has, only using $_row['total'] instead of mysql_num_rows Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted November 8, 2010 Share Posted November 8, 2010 I personally find that mysql_num_rows can be quite slow sometimes, especially when you are fetching hundreds of rows at a time (which I do frequently).. So option, adds a few lines of code. First off I add SQL_CALC_FOUND_ROWS into my query so: <?php $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM table"; ?> Then to retrieve that I add a minimum of 3 lines of code. $_query = "SELECT FOUND_ROWS() as total"; $_result = mysql_query($_query); $_row = mysql_fetch_array($_result, MYSQL_ASSOC); I tend to do my select statement outside of mysql_query just for my own ease of reading later. With this code I would then access count by using: $_row['total'] With that you'd then use the same type of if statement as in anti-moronic has, only using $_row['total'] instead of mysql_num_rows Interesting, didn't know that! Thanks for info. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.