Hello,
I am simply trying to query a database then spit the results on the screen using foreach. I use PHP Version 5.0.4 on apache2 with winxp
My query brings back 18 lines (user_id) but in my foreach statement it only shows the first record 2x!!
--------------------------------------------------------------------------------------------
$select_distinct = mysql_query("select distinct submissions.user_id from submissions, talent where submissions.talent_id = talent.talent_id and submissions.status='1' order by submissions.user_id desc");
$results = mysql_fetch_array($select_distinct);
$found3 = mysql_num_rows($select_distinct);
print "<p>".$found3." records found</p>";
if (is_array($results)) {print "<p>this is definitely an array</p>";}
foreach ($results as $value)
{
echo "Value: " . $value . "<br />";
}
---------------------------------------------------------------------------------------------------
the screen shows;
----------------------------------------------------------
18 records found
this is definitely an array
Value: 1676
Value: 1676
Help would sure be appreciated. I would like to see al 18 user_id's
thanks!!
this should be so easy! foreach looping through an array
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
- mysql_fetch_array() returns both a named and numericly index element of each field returned, by default.
- each call to a mysql_fetch_* function returns a single row of the result set.
- please use
Code: Select all
andCode: Select all
where appropriate when posting code.[/list]
PostPosted: Mon Sep 05, 2005 11:41 pm Post subject: this should be so easy! foreach looping through an array Reply with quote Edit/Delete this post
Since each call to a mysql_fetch_* function returns a single row of the result set how do I get the entire result set into an array so I can loop through it with foreach?
the screen shows;
Help would sure be appreciated. I would like to see al 18 user_id's
thanks!!
Since each call to a mysql_fetch_* function returns a single row of the result set how do I get the entire result set into an array so I can loop through it with foreach?
Code: Select all
$select_distinct = mysql_query("select distinct submissions.user_id from submissions, talent where submissions.talent_id = talent.talent_id and submissions.status='1' order by submissions.user_id desc");
$results = mysql_fetch_array($select_distinct);
$found3 = mysql_num_rows($select_distinct);
print "<p>".$found3." records found</p>";
if (is_array($results)) {print "<p>this is definitely an array</p>";}
foreach ($results as $value)
{
echo "Value: " . $value . "<br />";
}Code: Select all
18 records found
this is definitely an array
Value: 1676
Value: 1676Help would sure be appreciated. I would like to see al 18 user_id's
thanks!!
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
you use a loop to get each row from the record set, either generating output as you go, or storing the results into an array for later iterating.
Code: Select all
while($row = mysql_fetch_row($select_distinct))
{
// .........
}