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

496

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!

672

Answer

Solution:

Change yourSELECT 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!

People are also looking for solutions to the problem: php - Load html with ajax, but hide content until loaded

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.