0

I am trying to make an accordion (twitter bootstrap) displaying all of someone's friends, I end up just creating a infinate loop, basically all I am trying to do is echoing out a php string for each line in the results.

can anyone spot what I am doing stupid (other than not using mysqli)?

    while ( $rowresult = mysql_fetch_assoc(mysql_query("SELECT * FROM friends WHERE id='".$user["ID"]."' ")) ) {
        $auto = "0px";
        echo '<div class="accordion-group"><div class="accordion-heading"><div class="accordion-heading">';
        echo '<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapse'. $counter . '">';       
        echo $rowresult['firstname'];
        echo ' ';
        echo $rowresult['lastname'];
        echo ', ';
        echo $rowresult['headline'];
        echo '</a></div> <div id="collapse'. $counter .'" class="accordion-body ';
        if ($counter == 0){
        echo 'in ';
        $auto = "auto";
        }
        echo 'collapse" style="height: '. $auto .'; "><div class="accordion-inner">';
        echo '<img src="'.$rowresult["pictureUrl"].'">';        
        echo '</div></div></div></div>';    
        $counter++;
    }
1
  • 1
    mysql_ functions have begun to be deprecated. Read this for more information. Commented Sep 4, 2012 at 1:02

3 Answers 3

2

It doesn't work because you are executing the same query again and again.
Do that instead:

$res = mysql_query("SELECT * FROM friends WHERE id='".$user["ID"]."'");
while ( $rowresult = mysql_fetch_assoc($res) ) {
....
}

Also, I suggest you use MySQLi or PDO.

Sign up to request clarification or add additional context in comments.

Comments

1

You are repeating your query on every iteration. Move the mysql_query() out of the loop to a variable.

$q = mysql_query("SELECT * FROM friends WHERE id='".$user["ID"]."' ");
while ($rowresult = mysql_fetch_assoc($q)) {
    // ...
}

Comments

0

Put the query before the while loop. IE

 $q=mysql_query("SELECT * FROM friends WHERE id='".$user["ID"]."' ");
 while ( $rowresult = mysql_fetch_array($q)) { 

...

But then of course use PDO... :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.