By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,874 Members | 646 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,874 IT Pros & Developers. It's quick & easy.

If result set is empty print "no records"

100+
P: 155
I've tried to incorporate an IF Statement into the below so that if the query returned nothing - a message saying "There are no jobs currently listed" would be displayed, but for some reason it hasn't worked. Right now it just returns a blank table.

[PHP]<?php
$query="SELECT * FROM $nameid WHERE display_ad='yes' AND submitted > SUBDATE(NOW(), INTERVAL 30 DAY) ORDER BY submitted DESC LIMIT $offset, $rowsPerPage";
$result=mysql_query($query) or die('Error, query failed');
$num=mysql_numrows($result);

$i=0;
while ($i < $num)
{
$id=mysql_result($result,$i,"id");
$company=mysql_result($result,$i,"company");
$position=mysql_result($result,$i,"position");
$city=mysql_result($result,$i,"city");
$date=mysql_result($result,$i,"submitted");
$fdate = explode('-', $date);
?>

<table>
<tr>
<td width="35%"><? echo ("<A HREF='see_job.php?nameid=$nameid&file=$id'>$compan y</A>"); ?></td>

<td width="37%"><? echo "$position"; ?></td>

<td width="16%"><? echo "$city"; ?></td>

<td width="12%"><? echo "$fdate[1]/$fdate[2]/$fdate[0]"; ?></td>
</tr>
</table>

<?php
++$i;
} [/PHP]
Oct 2 '07 #1
Share this Question
Share on Google+
3 Replies

ak1dnar
Expert 100+
P: 1,584
First what is this "mysql_numrows" ? (4th line in the above code)

Then read here please:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $num=mysql_num_rows($result); 
  3. if($num > 0){
  4.     while($row=mysql_fetch_assoc($result)){
  5.     //Print The $row array elements here
  6.     echo $row['MySQL_COLUMN_NAME1'];
  7.     echo $row['MySQL_COLUMN_NAME2'];
  8.     echo $row['MySQL_COLUMN_NAME3'];
  9.     }
  10. }else{
  11. echo 'No-Records';
  12. }
  13. ?>
Oct 2 '07 #2

100+
P: 155
First what is this "mysql_numrows" ?
It's just something I've always used, but I like yours better. Works fine now, thanks!

David
Oct 2 '07 #3

ak1dnar
Expert 100+
P: 1,584
And David, If you ever need anything post back to the forum. Thanks.
Oct 3 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.