Jump to content

Foreach multiple results


Pain

Recommended Posts

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 by Pain
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.