PHP:从while语句开始,但只显示一次它而不重复每个mysql行

I'm not sure exactly what to search for to find my exact results. I am trying to create a system that retrieves data from database (no problem, this works). But I want to organize and minimize the amount of code for each section of the page. I am retrieving code via a MySQL query. I then set the $count = "0". Then I start my while statement $section = mysql_fetch_array($query) { $count++ ...

Here is the tricky part: I want to do two if ($count > 0) statements. First, I want to say that IF $count > 0, echo table and headers for the table columns. I don't want this to repeat inside the while statement. I want this to only appear once. Basically, excluding this from the loop, but still be inside the while statement, because I don't want two while statements if at all possible.

The second if $count > 0 will then start echoing out each row result.

Not sure If I'm just being dumb and this isn't possible. It's something I've wondered about for a while now. I hope you understand what I'm looking for and didn't waste your time.

 if (mysql_num_rows($result) > 0) {
     // print headers
     while ( $row = mysql_fetch_row($result)) {
           // display row
     }
  }

what you could do, is alter your query, so that it retrieves the number of returned rows as well, using the included Count() function.

Then, before the while loop starts, you retrive this value from your Resultset and if it is > 0 you print your table headers and enter the loop.

Hope this helps.