Acmlm's Board - I2 Archive - Programming - Weird PHP array problem
User | Post |
Vystrix Nexoth
Posts: 204/348 |
...$batusers['name'] is not an array either.
as discussed on IRC, what you were looking for was:$resultset = mysql_query ("SELECT hp, name, etc FROM users WHERE whatever"); while ($user = mysql_fetch_assoc ($resultset)) { echo $user['name']; } |
windwaker
Posts: 795/1797 |
Ah, okay, now I see.
So now I did foreach($batusers['name'] as $name), and it worked.
Thanks guys. |
Vystrix Nexoth
Posts: 202/348 |
foreach($batusers as $name){ echo $name['name']; } You are aware that $name is not an array, yes? You ought not treat it as though it were. |
windwaker
Posts: 793/1797 |
*ww is confused*
What do you mean, use a comma instead? That's exactly what print_r($batusers) echoed; I didn't make the array myself, it came from a mysql_query. |
Dylan
Posts: 1234/1407 |
Um, didn't you just post your erroneous array? Uh, use a comma instead of this deal =>
Eep! pre text! Table stretching! |
windwaker
Posts: 792/1797 |
Originally posted by Gywall
Originally posted by windwaker
$batusers = mysql_query("SELECT * FROM `users` WHERE `inbattle` = '". $batid[id] ."' ORDER BY `id` DESC LIMIT 10"); $batusers = mysql_fetch_array($batusers, MYSQL_ASSOC); foreach($batusers as $name){ echo $name['name']; }
This echoes "213wa5wa211320030153553". When I use print_r($batusers), I get
Array ( [id] => 2 [maxhp] => 100 [maxmp] => 30 [name] => windwaker [password] => ab17850978e36aaf6a2b8808f1ded971 [money] => 500 [email] => windwaker101@gmail.com [avatar] => avatars/locke.gif [level] => 2 [exp] => 1000 [hp] => 100 [mp] => 30 [weapon] => 2 [item1] => 0 [item2] => 0 [inbattle] => 35 [subweapon] => 0 [location] => 1 [attack] => 500 [turns] => 300 [magicpower] => 50 [defense] => 50 [speed] => 30 )
What's wrong here?
use , and not =>
It should make a difference. Hell, I've used arrays before.
Array ( [id] => 2 [maxhp] => 100 [maxmp] => 30 [name] => windwaker [password] => ab17850978e36aaf6a2b8808f1ded971 [money] => 500 [email] => windwaker101@gmail.com [avatar] => avatars/locke.gif [level] => 2 [exp] => 1000 [hp] => 100 [mp] => 30 [weapon] => 2 [item1] => 0 [item2] => 0 [inbattle] => 35 [subweapon] => 0 [location] => 1 [attack] => 500 [turns] => 300 [magicpower] => 50 [defense] => 50 [speed] => 30 ) <-- this is what is echoed by print_r; the array is created with:
$batusers = mysql_query("SELECT * FROM `users` WHERE `inbattle` = '". $batid[id] ."' ORDER BY `id` DESC LIMIT 10"); $batusers = mysql_fetch_array($batusers, MYSQL_ASSOC);
Dylan
Posts: 1233/1407 |
Originally posted by windwaker
Array ( [id] => 2 [maxhp] => 100 [maxmp] => 30 [name] => windwaker [password] => ab17850978e36aaf6a2b8808f1ded971 [money] => 500 [email] => windwaker101@gmail.com [avatar] => avatars/locke.gif [level] => 2 [exp] => 1000 [hp] => 100 [mp] => 30 [weapon] => 2 [item1] => 0 [item2] => 0 [inbattle] => 35 [subweapon] => 0 [location] => 1 [attack] => 500 [turns] => 300 [magicpower] => 50 [defense] => 50 [speed] => 30 )
What's wrong here?
An array consists of a key and a value. Like: $this = array( 'key' => 'value', 'key' => 'value', 'key' => 'value' ); But you don't always need a key, its just an identifier associated with a value. So if you just have the value, use commas. Like: $this = array('value', value', 'value'); |
Gywall
Posts: 291/356 |
Originally posted by windwaker
$batusers = mysql_query("SELECT * FROM `users` WHERE `inbattle` = '". $batid[id] ."' ORDER BY `id` DESC LIMIT 10"); $batusers = mysql_fetch_array($batusers, MYSQL_ASSOC); foreach($batusers as $name){ echo $name['name']; }
This echoes "213wa5wa211320030153553". When I use print_r($batusers), I get
Array ( [id] => 2 [maxhp] => 100 [maxmp] => 30 [name] => windwaker [password] => ab17850978e36aaf6a2b8808f1ded971 [money] => 500 [email] => windwaker101@gmail.com [avatar] => avatars/locke.gif [level] => 2 [exp] => 1000 [hp] => 100 [mp] => 30 [weapon] => 2 [item1] => 0 [item2] => 0 [inbattle] => 35 [subweapon] => 0 [location] => 1 [attack] => 500 [turns] => 300 [magicpower] => 50 [defense] => 50 [speed] => 30 )
What's wrong here?
use , and not =>
It should make a difference. Hell, I've used arrays before. |
windwaker
Posts: 789/1797 |
$batusers = mysql_query("SELECT * FROM `users` WHERE `inbattle` = '". $batid[id] ."' ORDER BY `id` DESC LIMIT 10"); $batusers = mysql_fetch_array($batusers, MYSQL_ASSOC); foreach($batusers as $name){ echo $name['name']; }
This echoes "213wa5wa211320030153553". When I use print_r($batusers), I get
Array ( [id] => 2 [maxhp] => 100 [maxmp] => 30 [name] => windwaker [password] => ab17850978e36aaf6a2b8808f1ded971 [money] => 500 [email] => windwaker101@gmail.com [avatar] => avatars/locke.gif [level] => 2 [exp] => 1000 [hp] => 100 [mp] => 30 [weapon] => 2 [item1] => 0 [item2] => 0 [inbattle] => 35 [subweapon] => 0 [location] => 1 [attack] => 500 [turns] => 300 [magicpower] => 50 [defense] => 50 [speed] => 30 )
What's wrong here? |
|
|