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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

lost in an array... 1

Status
Not open for further replies.

admoore

IS-IT--Management
May 17, 2002
224
US
I have multiple queries which return results which I need to access in a single for loop, rather than in a while[\i] loop... I am displaying a multi-column report where some columns have far fewer results than others...

My idea was to write the result of each applicable query into an array, determine which array has the highest count, and use that value in a for loop...

Problem is, I am apparently not creating an array I can loop thorough numerically...

sample code...
Code:
  $query = "SELECT year, Count(year) AS year_count
                                    FROM list_selection
                                    GROUP BY year
                                    ORDER BY year desc";

$result = mysql_query($query,$link);
$year_array = mysql_fetch_array($result);

for($i = 0;$i<count($year_array);$i++){
echo "$year_array[$i] <br />";
}

I just need to better understand the array returning to PHP from the query...
 
This would be a PHP question, not so much a MYSQL question.

In any case,
PHP.net said:
mysql_fetch_array
Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

I other words: mysql_fetch_array returns an array of each row your query produces. However, each call only produces a single row in the form of an array.
$row['column1']
$row['column2']
etc...

Where each indexis either the name of the column, or a number between 0 and the total number of columns requested in your query.
It does not produce an array of the entire result set returned by your query.
The while loop usually associated with this, is used to run through all the rows returned by your query, for that reason.

You can use the the while loop to insert your results into another array if you need to.

Code:
while($rows[]=mysql_fetch_array($result));
Then you could run through the $rows array with a for loop.

Code:
for($i=0;$i<=count($rows);$i++){
$rows[i]['column1']
or $rows[i]['column2']
etc...
}

forum434 for any further PHP questions.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top