I have a query which returns some results, and then I’m using a while loop to output the results.
The problem I’m having is that I want to return the ‘question’ before the while loop so that it doesn’t display a number of times inside the loop.
I have the following code:
$query = "SELECT * FROM polls LEFT JOIN pollanswers ON polls.pollID = pollanswers.pollID WHERE polls.pollID = 1 ORDER By pollAnswerListing ASC";
$result = mysql_query($query);
echo '<p>Question</p>';
echo '<form action="#" class="poll-form">';
echo ' <p class="error"></p>';
echo ' <fieldset>';
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<div class="row">';
echo '<input type="radio" name="poll" id="'.$row["pollAnswerID"].'" class="radio" value="'.$row["pollAnswerID"].'" /><label for="'.$row["pollAnswerID"].'">'.$row["pollAnswerValue"].'</label>';
echo '</div>';
}
echo ' </fieldset>';
echo '</form>';
As you can see the loop is working perfectly, but how do I output the question just once from the same SQL statement BEFORE outputting the loop?
You can fetch one row to display your question (assuming you have the same question value for multiple rows), then use
mysql_data_seek()to rewind the result resource back to the first record to start your loop: