Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multi-dimensional array not working

Status
Not open for further replies.

y2k1981

Programmer
Aug 2, 2002
773
0
0
IE
I'm using PHP to pull some data my sql like so:
Code:
$get_users = ("SELECT * FROM employees");
while($users = mysql_fetch_array($get_users))
{
echo $users['usernmae'];
--- OR ---
echo $users[0]
}
I have two questions, firstly, shouldn't the following work:
Code:
$get_users = mysql_query("SELECT * FROM employees");
$users = mysql_fetch_arrau($get_users);
for($i=0; $i < count($users); $i++)
{
echo $users[$i][0]
}
and also, what I want to do is echo out the following [0][username], [1][username2] etc etc etc. So what I want to do is use while($users = mysql_fetch_array($get_users)) but also have a for loop which iterates $i by one each time. how can I go about doing this?

sorry if this sounds confusing
 
First, you have to actually run the query to get data back from the database. To see all the required steps, take a look at the example code on the PHP online manual page entry for mysql_fetch_array().

Second, the return from mysql_fetch_array() will not be a multidimensional array. Each invocation of mysql_fetch_array() returns one row from the returned dataset.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I know that you have to run the query in order to get any results, that's not the problem. I understand what you're saying about mysql_fetch_array(). that makes sense. So, what I really need to know now is how do I use
while($users = mysql_fetch_array($get_users))
and also have a for loop that says for($i = 0; $i < count($users); $i++) so that I can literally echo the following:
myArray[0]['username1']
myArray[1]['username2']
...
myArray[0]['username10']

thanks for your help
 
yes, I want to echo it out into a javascript array
 
I'm such a fool ... I don't know why I didn't think of this sooner:
Code:
$get_users = mysql_query("SELECT * FROM users");
$i=0;

while($users = mysql_fetch_array($get_users))
  {
     echo "myArray[".$i."][".$users['userName']."]";
     $i++;
   }

Thanks again for all your help
 
You probably want to use mysql_fetch_assoc() instead of mysql_fetch_array().
Code:
$tmp = array();
while ($rw = mysql_fetch_assoc($get_users))
   $tmp[] = $rw;
echo '<pre>';print_r($tmp);echo '</pre>'; // debug -- see what's in the $tmp array
This should give you an array like:
$tmp[0]['username']
$tmp[1]['username']

Try it and see if it gives you what you want.

Why do you want a Javascript array? What can that do that can't be done with PHP?

Ken
 
That isn't going to put the data into the $myArray array.

Something like this:

Code:
$get_users = mysql_query("SELECT * FROM users");
$myArray = array();
while ($users = mysql_fetch_[red]assoc[/red]($get_users))
{
   $myArray[] = $users;
}

print_r ($myArray);

will.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I didn't need to have it put into a PHP array, just a JS array by echo-ing out the output in the appropriate place between the script tags. but thanks for your method, its good to know
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top