• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

how to determain at least one row is returned from a DB table?

Hello,

I want to print a html table only if there is at least one row can be returned from a table.
Foe example, something like:
if ($rowNum >0)
//then  print a html table
< table> .....

I guess I need to use mysql_num_rows () to determin the $rowNum. Is this a only choice? or there is a better choice in terms of performance?

Thanks very much for your reply,
lilyyan
0
lilyyan
Asked:
lilyyan
  • 3
2 Solutions
 
Richard QuadlingSenior Software DeverloperCommented:
If the query is a SELECT statement then mysql_num_rows() is the correct function to use.

If you are updating/inserting/deleting rows, then mysql_affected_rows() is the correct function.
0
 
Richard QuadlingSenior Software DeverloperCommented:
Another option is something like this ...

<?php-not-quite

$r_Conn = mysql_pconnect(...);
$s_SQL = 'Some query';
$r_Result = mysql_query($s_SQL);
$s_Rows = '';
while(False !== ($a_Row = mysql_fetch_assoc($r_Result)))
 {
 $s_Rows .= 'Add a TR to the list of rows';
 }
mysql_free_results($r_Result);
$s_Table = ('' !== $s_Rows) ? 'Table header' + $s_Rows + 'table footer' : '';
echo $s_Table;

?>

sort of thing.

Basically, use a loop to build the output you want into a string.

Outside of the loop, if the string contains anything, then there was data.
0
 
Richard QuadlingSenior Software DeverloperCommented:
If you think that the majority of the time there WILL be data, then you are going to want to process all the rows.

If not, then 1 call (mysql_fetch_assoc() vs mysql_num_rows()) is pretty irrelevant.

0
 
lilyyanAuthor Commented:
Hi, thanks so mcuh for your reply. Regards.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now