Jump to content

MySQL Array Issues


kickassamd

Recommended Posts

Im having problems reading from an array via mysql_fetch_array()

the function ================
[code]
getBoth($result)
{
   while ($dbArray = @mysql_fetch_array($result, MYSQL_BOTH))
   {
       $returnArray[] = $dbArray;
   }
return $returnArray;
}
[/code]

if i print_r(getBoth($queryResult)) i get this ============
[code]
Array ( [0] => Array ( [0] => Stubbs [char_name] => Stubbs ) [1] => Array ( [0] => Ic3m4n [char_name] => Ic3m4n ) [2] => Array ( [0] => Bawlz [char_name] => Bawlz ) [3] => Array ( [0] => CrispinxLongbow [char_name] => CrispinxLongbow ) [4] => Array ( [0] => Lilice [char_name] => Lilice ) )
[/code]

If i use [code]
foreach ($online as $key => $name)
{
                  echo $name;
}
[/code]
it just prints ==============
[code]
ArrayArrayArrayArrayArray
[/code]

So then i have to [code]
foreach ($online as $key => $name)
{
foreach ($name as $player)
{
echo $player."<br>";
}
}
[/code]

To print each name to the screen..... is there something I am doing wrong... 2 foreach loops to print an array seems wrong
Link to comment
Share on other sites

Well just looking at this array

Array ( [0] => Array ( [0] => Stubbs [char_name] => Stubbs ) [1] => Array ( [0] => Ic3m4n [char_name] => Ic3m4n ) [2] => Array ( [0] => Bawlz [char_name] => Bawlz ) [3] => Array ( [0] => CrispinxLongbow [char_name] => CrispinxLongbow ) [4] => Array ( [0] => Lilice [char_name] => Lilice ) )

your foreach $key=>$name would give you

  $key=0
  $name=Array ( [0] => Stubbs [char_name] => Stubbs )

So you would need to do $name[0] or $name['char_name']

this also means you do not need the key so you can do

foreach($online AS $name){
  echo $name['char_name'];
}

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.