this should be so easy! foreach looping through an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
laurus
Forum Newbie
Posts: 3
Joined: Mon Sep 05, 2005 8:52 pm
Contact:

this should be so easy! foreach looping through an array

Post by laurus »

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!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. mysql_fetch_array() returns both a named and numericly index element of each field returned, by default.
  2. each call to a mysql_fetch_* function returns a single row of the result set.
  3. please use

    Code: Select all

    and

    Code: Select all

    where appropriate when posting code.[/list]
laurus
Forum Newbie
Posts: 3
Joined: Mon Sep 05, 2005 8:52 pm
Contact:

Post by laurus »

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?

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 />";
}
the screen shows;

Code: Select all

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!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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))
{
  // .........
}
Post Reply