如何从mysql输出两列值并循环其余部分

In php working with mysql, how can I output two columns values only once, and then loop thru the others.

Here:

$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
while($row = mysql_fetch_array($exe_fetch_data))
echo $row['category'];    //Only once
echo $row['subcategory']; //Only once

{
echo $row['title'];
echo $row['body'];
echo $row['views'];
}

Desired output:

Category->SubCategory //Outputted only once

Title,  //Title Body and Views depend on whatever's there in the db. Could be 10 or more
Body,
Views

Title,
Body,
Views

Title,
Body,
Views

This code shoud do the trick

    $exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
    $row_once = mysql_fetch_array($exe_fetch_data)
    echo $row_once['category'];    //Only once
    echo $row_once['subcategory']; //Only once

   while($row = mysql_fetch_array($exe_fetch_data)) {
    echo $row['title'];
    echo $row['body'];
    echo $row['views'];
    }

You could also try using a flag whose value is initially true and will turn false after first iteration. But I think above code will be faster than using a flag as it wont compare/check flag value after each iteration and with a huge database it will save execution time.

One solution might be a bool that remembers if the category has been outputed already:

$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
$category_output_done = false;
while($row = mysql_fetch_array($exe_fetch_data))
{
    if (!$category_output_done) {
        echo $row['category'];    //Only once
        echo $row['subcategory']; //Only once
        $category_output_done = true;
    }

    echo $row['title'];
    echo $row['body'];
    echo $row['views'];
}

Another solution could be a counter that increments on every iteration.

Add a simple counter-

$counter = 0;
$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
while($row = mysql_fetch_array($exe_fetch_data))
if ($counter == 0){
echo $row['category'];    //Only once
echo $row['subcategory']; //Only once
$counter = 1;
}

As you might have lots of results, keeping a flag in your while loop whether you have displayed the categories already consumes CPU. As you are always showing the categories with the first result set I would not keep a flag in the while loop but do it like this:

$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
$iNrOfResults = mysql_num_rows($exe_fetch_data);
//See if the result set has any rows at all, display the categories once
if(iNrOfResults > 0)
{
    $row = mysql_fetch_array($exe_fetch_data)
    echo $row['category'];    //Only once
    echo $row['subcategory']; //Only once
    echo $row['title'];
    echo $row['body'];
    echo $row['views'];
}

//See if there are any more results, do not display the categories anymore
if(iNrOfResults > 1)
{
    while($row = mysql_fetch_array($exe_fetch_data))
    {
        echo $row['title'];
        echo $row['body'];
        echo $row['views'];
    }
}