473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Foreach() returns single row

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
8 7315
Dormilich
8,658 Expert Mod 8TB
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
flydev
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
5,058 Expert 4TB
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
8,658 Expert Mod 8TB
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
flydev
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
8,658 Expert Mod 8TB
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
5,058 Expert 4TB
Yes. You will have to build a two-dimensional array to represent the table, like my example shows.
Oct 19 '08 #8
flydev
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

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

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
13
by: cody | last post by:
foreach does implicitly cast every object in the collection to the specified taget type without warning. Without generics this behaviour had the advantage of less typing for us since casting was...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
11
by: Wilfried Mestdagh | last post by:
Hi, does foreach guarantees the sequence order starting from index 0 ? -- rgds, Wilfried http://www.mestdagh.biz
9
by: garyusenet | last post by:
I'm a bit confused about the differences of these two commands (what is the right word for commands here?) when used to enumerate the contents of an array. The below example uses both foreach...
13
by: Tamagafk | last post by:
Hi! Looks like there is a bug in php. If I have function which uses foreach to run trough array recursively, the lop-level foreach interupted by lover-level foreach'es. If I use simply 'for'...
5
by: james | last post by:
Hey Guys, Would anyone mind explaining why a foreach will implicitly do unsafe casts, and if there is a way to turn this off? Here is an example: ulong vals = new ulong { ulong.MaxValue };...
14
by: jehugaleahsa | last post by:
I have a rather complex need. I have a class that parses web pages and extracts all relevant file addresses. It allows me to download every pdf on a web page, for instance. I would like to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.