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
https://forums.phpfreaks.com/topic/29163-mysql-array-issues/
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
https://forums.phpfreaks.com/topic/29163-mysql-array-issues/#findComment-133813
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.