Array question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ms_tilly
Forum Newbie
Posts: 6
Joined: Sun Mar 19, 2006 6:52 am

Array question

Post by ms_tilly »

hiya,

in this array

Code: Select all

while($row = mysql_fetch_assoc($result)) {
            $data[] = $row;
		}
		
			print_r($data);
why do i get this

Code: Select all

Array ( [0] => Array ( [u_id] => 14 [name] => Smith ) )
and not

Code: Select all

Array ( [u_id] => 14 [name] => Smith )

thanks for your help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$row is an array. You're adding an entry to another array ($data.)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Array question

Post by Benjamin »

Code: Select all

$Counter = 0;
while($data[$Counter] = mysql_fetch_assoc($result)) { $Counter++; }

print_r($data[0]);  // prints the first row array
print_r($data); // prints all row arrays
ms_tilly
Forum Newbie
Posts: 6
Joined: Sun Mar 19, 2006 6:52 am

Post by ms_tilly »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


so i could get around by getting rid of the while

Code: Select all

$row = mysql_fetch_assoc($result);
          
        
            print_r($row);
and then i can reference that like...

Code: Select all

$usernam = $row[name];
is that right?


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basically, yes.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

$Data = mysql_fetch_assoc($link);

$username = $Data['name']);
Post Reply