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

Posted on 2007-07-23
Last Modified: 2007-07-23

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,
Question by:lilyyan
    LVL 40

    Accepted Solution

    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.
    LVL 40

    Expert Comment

    Another option is something like this ...


    $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';
    $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.
    LVL 40

    Assisted Solution

    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.


    Author Comment

    Hi, thanks so mcuh for your reply. Regards.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now