Jump to content

query in a loop issues


thefortrees

Recommended Posts

I am creating a survey that pulls questions and responses from a DB. When I query the DB for each question and response from the mysql command line, all the desired data are returned.

 

As soon as the query is implemented into the PHP code below, the first response that should have been retrieved from the DB is not. (There are multiple responses to each question - it does not retrieve the first response for the first question, but all responses after that are retrieved.

 

$query = "SELECT question, response, position FROM questions, responses, survey_instances " .
		"WHERE questions.questions_id = responses.questions_questions_id " .
		"AND questions.questions_id = survey_instances.questions_questions_id ORDER BY position";

$result = mysql_query($query);
$row = mysql_fetch_array($result);

$responses = array();

//Print out questions. If position/question has already been echoed, do not echo again
//so that one question is not echoed multiple times.	
while ($row = mysql_fetch_array($result)){

	if ( !in_array($row['question'], $responses) ){
		echo
			"<table>" .  
			"<tr>" . $row['position'] . ") " . $row['question'] . "</tr>" .
			"<tr><td>" . $row['response'] . "</tr></td>";

		$responses[] = $row['question'];
	}

	echo 
		"<tr><td>" . $row['response'] . "</td>" .
		"</table><br>";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.