Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Weird PHP array problem | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
windwaker

Ball and Chain Trooper
WHY ALL THE MAYONNAISE HATE
Level: 61

Posts: 789/1797
EXP: 1860597
For next: 15999

Since: 03-15-04

Since last post: 4 days
Last activity: 6 days
Posted on 12-24-04 10:14 PM Link | Quote
$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?
Gywall

Silver axe
Level: 30

Posts: 291/356
EXP: 164069
For next: 1800

Since: 03-15-04
From: In front of my moniter, where you're not!

Since last post: 104 days
Last activity: 14 hours
Posted on 12-24-04 11:53 PM Link | Quote
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.
Dylan
Devil Trumpets and Angel Trombones ~
Level: 54

Posts: 1233/1407
EXP: 1181697
For next: 52173

Since: 06-19-04
From: Ottawa, Canada.

Since last post: 1 day
Last activity: 6 hours
Posted on 12-25-04 12:15 AM Link | Quote
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');
windwaker

Ball and Chain Trooper
WHY ALL THE MAYONNAISE HATE
Level: 61

Posts: 792/1797
EXP: 1860597
For next: 15999

Since: 03-15-04

Since last post: 4 days
Last activity: 6 days
Posted on 12-25-04 12:50 AM Link | Quote
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
Devil Trumpets and Angel Trombones ~
Level: 54

Posts: 1234/1407
EXP: 1181697
For next: 52173

Since: 06-19-04
From: Ottawa, Canada.

Since last post: 1 day
Last activity: 6 hours
Posted on 12-25-04 02:17 AM Link | Quote
Um, didn't you just post your erroneous array? Uh, use a comma instead of this deal =>

Eep! pre text! Table stretching!


(edited by Dylan on 12-24-04 05:18 PM)
windwaker

Ball and Chain Trooper
WHY ALL THE MAYONNAISE HATE
Level: 61

Posts: 793/1797
EXP: 1860597
For next: 15999

Since: 03-15-04

Since last post: 4 days
Last activity: 6 days
Posted on 12-25-04 03:17 AM Link | Quote
*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.
Vystrix Nexoth

Level: 30

Posts: 202/348
EXP: 158678
For next: 7191

Since: 03-15-04
From: somewhere between anima and animus

Since last post: 3 days
Last activity: 2 days
Posted on 12-25-04 03:58 AM Link | Quote
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

Ball and Chain Trooper
WHY ALL THE MAYONNAISE HATE
Level: 61

Posts: 795/1797
EXP: 1860597
For next: 15999

Since: 03-15-04

Since last post: 4 days
Last activity: 6 days
Posted on 12-25-04 04:02 AM Link | Quote
Ah, okay, now I see.

So now I did foreach($batusers['name'] as $name), and it worked.

Thanks guys.
Vystrix Nexoth

Level: 30

Posts: 204/348
EXP: 158678
For next: 7191

Since: 03-15-04
From: somewhere between anima and animus

Since last post: 3 days
Last activity: 2 days
Posted on 12-25-04 04:32 AM Link | Quote
...$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'];
}
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Weird PHP array problem | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.010 seconds.