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

Foreach() returns single row

P: 33
Hey guys, I hardly ever work with Foreach(), but I've read over the procedures for it and I cant seem to figure out why this is only returning a single record. I could be way off base on the proper use here. Note this is test code for trying to figure out whats going on, so none of the function elements are being used, but it illustrates my problem.

The array is storing the entire table with 18 columns and 3 rows.

Here is the function being called:
Expand|Select|Wrap|Line Numbers
  1. function topPilots($startTime, $endTime, $orderBy) // Retrieves list of pilots order
  2. {
  3.     $sql = "SELECT * FROM completedFlights";
  4.     $result = mysql_query($sql);
  5.  
  6.     $array = mysql_fetch_array($result, MYSQL_ASSOC);
  7.  
  8.     return $array;
  9. }
Here is the function in use:
Expand|Select|Wrap|Line Numbers
  1. $array = topPilots($thisMonthStart, time(), "SUM(cargo)");
  2.  
  3. foreach($array as $key => $value)
  4. {
  5.     echo $key.": ".$value." <br />";
  6. }
Oct 19 '08 #1
Share this Question
Share on Google+
8 Replies


Dormilich
Expert Mod 5K+
P: 8,639
since this is a test case it's not easy to tell.
first make sure you actually have more than one entry in $array (use var_dump($array)).
second, is "SUM(cargo)" supposed to be a string?

regards
Oct 19 '08 #2

P: 33
SUM(cargo) is a string that is inputted into the function topPilots, but it is not being used in this case as the function does not use any of the elements. I did an var_dump($array) and it only brought up the single row. I suppose its a problem with the function...

Will returning an array in a function do this?
Oct 19 '08 #3

Atli
Expert 5K+
P: 5,058
Hi.

mysql_fetch_array will only retrieve a single row from your result set, storing the fields in an array.

To get the entire table into a array, you would have to do something like:
Expand|Select|Wrap|Line Numbers
  1. $table = array();
  2. while($row = mysql_fetch_array($result)) {
  3.   $table[] = $row;
  4. }
  5.  
Also, rather than using mysql_fetch_array and pass MYSQL_ASSOC, you could simply use the mysql_fetch_assoc function.
Oct 19 '08 #4

Dormilich
Expert Mod 5K+
P: 8,639
I did an var_dump($array) and it only brought up the single row. I suppose its a problem with the function...
could be as well a problem with the SQL or the database.
Oct 19 '08 #5

P: 33
Hi.

mysql_fetch_array will only retrieve a single row from your result set, storing the fields in an array.

To get the entire table into a array, you would have to do something like:
Expand|Select|Wrap|Line Numbers
  1. $table = array();
  2. while($row = mysql_fetch_array($result)) {
  3.   $table[] = $row;
  4. }
  5.  

Ahhh, thats the key...so I will have to build a new array using a WHILE() loop inside the function, and then return that new array?
Oct 19 '08 #6

Dormilich
Expert Mod 5K+
P: 8,639
Ahhh, thats the key...so I will have to build a new array using a WHILE() loop inside the function, and then return that new array?
yep.                    
Oct 19 '08 #7

Atli
Expert 5K+
P: 5,058
Yes. You will have to build a two-dimensional array to represent the table, like my example shows.
Oct 19 '08 #8

P: 33
Thanks guys! I dont know why I had it stuck in my head that WHILE() is used to loop through an existing array...i forget you pass a query when using it. Thanks again!
Oct 19 '08 #9

Post your reply

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