Zanrok
Zanrok

Reputation: 297

How can I display results from database using for each loop to cycle through IDs

So I am trying to display answers to a survey but first I want to separate the USER ids from each other. Right now Answers are stored similar to this....

SGpID, SGresult, SGFACemail

Essentially, I want to display a button for each Unique ID. So if I have 5 answers with an ID of 10, and 5 answers with an ID of 12. I want buttons saying "Results for ID 10" and "Results for ID 12".

What it is currently doing is displaying EVERY iteration where the select statement matches so essentially every answer... no bueno.

My thought was that I would go through a for each loop to separate the results and do something along the lines of...

"Each time SGpID is different", display a new button with that new SGpID.

I am just not sure really how to write it out. Here is what I have so far for the statement.

$query_db = ("SELECT SGpID FROM SGresult WHERE SGFACemail = '$email'");
$result = mysql_query($query_db) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
//insert for each loop here?
echo $row['SGpID'];
}

Thanks as always for any help or ideas on this one!

Upvotes: 0

Views: 386

Answers (1)

eggyal
eggyal

Reputation: 125835

Change your SELECT command to:

SELECT DISTINCT SGpID FROM SGresult WHERE SGFACemail = ?

Note I have used ? in place of '$email' as you really should use prepared statements. Learn from Bobby Tables!

Upvotes: 1

Related Questions