By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,631 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,631 IT Pros & Developers. It's quick & easy.

how to Get Data from database to array

abdoelmasry
100+
P: 104
Hi All
Could any one help me on this code PLZ ..
i make function to get data from database then
set data in array then return the array value

This is the Code:

function gtusersdata(){
$gtdata=mysql_query("select id,username,password from usersinformation");
while($cdata=mysql_fetch_row($gtdata)){
$userdata=array($cdata[0]=>array(username=>$cdata[1],password=>$cdata[2]));
}
return $userdata;
}


This is calling function:

$userdata=gtusersdata();

i want to show user data that have id=1

echo $userdata[1][username]."<br>".$userdata[1][password];

This Code Is not working
If You please i want to know WHY ??


i know that i can pass the user id to function and use

where id='userid';

but i want to return all users data not only one user

Abdo Elmas®y
Feb 10 '07 #1
Share this Question
Share on Google+
2 Replies


ronverdonk
Expert 2.5K+
P: 4,258
You have hardcoded
Expand|Select|Wrap|Line Numbers
  1. echo $userdata[1][username]."<br>".$userdata[1][password];
.
But how do you know that $userdata[1] exists?

The assignment statement [php]$userdata=array($cdata[0]=>array(username=>$cdata[1],password=>$cdata[2]));[/php] refills the $userdata array each time a new row is fetched. So when you have 3 rows in your table, only $userdata[3] exists and $userdata[1] is nowhere to be found!

The way to store all your rows in an array without losing anything is to [php]
REPLACE THIS STATEMENT
$userdata=array($cdata[0]=>array(username=>$cdata[1],password=>$cdata[2]));

BY THESE STATEMENTS:
$idx = $cdata[0];
$userdata[$idx]=array(fname=>$cdata[1],lname=>$cdata[2]);
[/php]

Ronald :cool:
Feb 11 '07 #2

abdoelmasry
100+
P: 104
Thank You Man
Ya Thats I Realy Need

AbdoElmas®y
Feb 12 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.