Pain Posted May 21, 2013 Share Posted May 21, 2013 (edited) I am trying to retrieve multiple values from the db and then display them, however the way i'm doing it seems wrong. <?php $query_members = "SELECT username, id FROM members LIMIT 10"; $result = $db->run($query_members); $i = 0; foreach($result as $username[$i]){ echo "<br />" . implode(" ", $username[$i]); $i++; } ?> This way one variable holds two values - username and id. How can i assign those values to two different variables? Thanks! Edited May 21, 2013 by Pain Quote Link to comment Share on other sites More sharing options...
Barand Posted May 21, 2013 Share Posted May 21, 2013 You need to fetch the rows from the result then echo the field values from those rows. Quote Link to comment Share on other sites More sharing options...
theverychap Posted May 21, 2013 Share Posted May 21, 2013 Your foreach is wrong. <?php $query_members = "SELECT username, id FROM members LIMIT 10"; $result = $db->run($query_members); // if $result comes back as an array... foreach ($result as $user) { echo 'username: ' .$user['name'] .'<br/>'; echo 'id: ' .$user['id'] .'<br/>'; } // ...or as an object foreach ($result as $user) { echo 'username: ' .$user->name .'<br/>'; echo 'id: ' .$user->id .'<br/>'; } ?> Hope that helps. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted May 21, 2013 Share Posted May 21, 2013 Your foreach is wrong. <?php $query_members = "SELECT username, id FROM members LIMIT 10"; $result = $db->run($query_members); // if $result comes back as an array... foreach ($result as $user) { echo 'username: ' .$user['name'] .'<br/>'; echo 'id: ' .$user['id'] .'<br/>'; } // ...or as an object foreach ($result as $user) { echo 'username: ' .$user->name .'<br/>'; echo 'id: ' .$user->id .'<br/>'; } ?> Hope that helps. You assume that an associative array is being returned, even though the code in the original post shows a numerical one. Also, just throwing answers at people doesn't help them to learn how to solve the problem, you really should include some information explaining why your code would be the right way to do things. Quote Link to comment Share on other sites More sharing options...
theverychap Posted May 21, 2013 Share Posted May 21, 2013 I specifically added array and object ways, i didn't know what was in $result. Anyhow, after re-reading the question: "This way one variable holds two values - username and id. How can i assign those values to two different variables?" It would be helpful to see what is returned into $result. Maybe look at list if you want to assign values to different variables. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.