0

I want to ask a question.i want to show data in table from database..it works but if no data in database, how i can show "there is no data yet" this is my code :

<table>
<thead>
<th>Test Name</th>
<th>Test Description</th>
</thead>

<?php while ($rec2= mysql_fetch_assoc($result2)) {
?>
<tr>
<td><?php echo $rec2['testName'];?></td>
<td><?php echo $rec2['testDesc'];?></td>
</tr>
<?php } ?>
</table>
Lisa
  • 9
  • 6

4 Answers4

3
if(mysql_num_rows($result2)==0)
   echo "Nothing" ;  

How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1
<?php 

if(mysql_num_rows($result2)>0)
{

?>
<table>
<thead>
<th>Test Name</th>
<th>Test Description</th>
</thead>

<?php while ($rec2= mysql_fetch_assoc($result2)) {
?>
<tr>
<td><?php echo $rec2['testName'];?></td>
<td><?php echo $rec2['testDesc'];?></td>
</tr>
<?php } ?>
</table>
<?php

}
else
{
?>
    NO DATA FOUND 
<?php
}
?>

Hope this will work for you.

Tristup
  • 3,603
  • 1
  • 14
  • 26
0

You check if your query return a result before fetching it:

if($result2){
    //fetch $result here
}
else{
    echo "there is no data yet";
}
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
Verhaeren
  • 1,661
  • 9
  • 10
  • 1
    That will be `false` only on an error. And `0 rows` is not an error; hence that check will not work – Hanky Panky Dec 12 '14 at 07:46
  • 1
    A downvote 2 seconds after posting the answer. I should call guiness. – Verhaeren Dec 12 '14 at 07:46
  • If the query executed fine and there are 0 rows in the result, it will still go to your `if` section – Hanky Panky Dec 12 '14 at 07:51
  • @Hanky웃Panky I thought that was the point: if the OP wants to know if the table is empty he/she will have to addapt his/her query accordingly `SELECT COUNT(*) FROM myTable`. – Verhaeren Dec 12 '14 at 07:55
  • Fair enough. `SELECT COUNT(*)` will also work you are right but then it requires another query. `mysql_num_rows` will do both things in one query. But my point is that if this solution is used as it is and there are 0 rows in the result (provided query doesnt have errors) user will not see the message "there is no data yet" – Hanky Panky Dec 12 '14 at 07:58
  • @Hanky웃Panky `mysql_num_rows` will return the number of rows that the query got, so if the query is `SELECT * FROM myTable WHERE myColumn = 'something'` for example, that will mean that there is any entry that meets that cryteria, not that the table is necesarily empty. My answer is wrong though. I know that. I got confused because I'm used to having a class to perform the queries and I forgot that I set up the method to return false when there is a `$result = 0`. Still was funny the quick downvote. I hit the submit button and a second later -1 lol. – Verhaeren Dec 12 '14 at 08:04
  • I'll be honest, that was me. After this discussion i believe the down-vote is no longer necessary. Just removed it. Cheers :) – Hanky Panky Dec 12 '14 at 08:13
  • @Hanky웃Panky Thankyou. I know when I make a mistake. And if an answer is wrong, it doesn't bother me. Cheers :) – Verhaeren Dec 12 '14 at 08:14
0
<?php
if(mysql_num_rows($result2)==0)
   echo "Nothing to Display" ;
else
{
?>
<table>
<?php 
while ($rec2= mysql_fetch_assoc($result2)) 
{
?>
<tr>
<td><?php echo $rec2['testName'];?></td>
<td><?php echo $rec2['testDesc'];?></td>
</tr>
<?php 
}//end while loop
?>
</table>
<?php
}//ending if-else
?>