Link to home
Start Free TrialLog in
Avatar of lilyyan
lilyyan

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lilyyan
lilyyan

ASKER

Hi, thanks so mcuh for your reply. Regards.