Jump to content

Why is my foreach loop not working with this array?


stj5353

Recommended Posts

Hi brains... I must be missing some core concept here that I hope someone can set me straight on..

 

I have a database query that returns say 8 rows. Here's how I know...

 

$link6_result10 = mysql_query($link6_sql10) or die("Link6 SQL10 Failed: Function drawInventory: " . mysql_error());

        $link6_rows10 = mysql_num_rows($link6_result10);

        $link6_array10 = mysql_fetch_array($link6_result10, MYSQLI_ASSOC);

        echo "You have $link6_rows10 items in your inventory."; <<---- returns 8

 

 

So why in the world does this not work??? What is the flaw in my logic?

 

$srchStr = null;

    foreach ($link6_array10 as $sn) {

    $srchStr .= " SerialNumber = '$sn' OR";

    echo "SN is $sn ";

    }

 

The only echo output of the foreach function is "SN is 12345" Not the 8 rows of content that I would expect. isn't this how foreach loops should work? Why do I not see "SN is 12345 SN is 23456 SN is 34567 etc..."????? I'm confused.

 

Thanks guys for any help.

 

Thanks for the suggestion! While I do get while loops, I'm not sure why the foreach function is failing. It seems so much more elegant to me.. I thought I understood foreach, but apparently I don't! Is there something I'm doing wrong? I doubt there is a bug with such a standard foreach() function...

 

Hi brains... I must be missing some core concept here that I hope someone can set me straight on..

 

I have a database query that returns say 8 rows. Here's how I know...

 

$link6_result10 = mysql_query($link6_sql10) or die("Link6 SQL10 Failed: Function drawInventory: " . mysql_error());

        $link6_rows10 = mysql_num_rows($link6_result10);

        $link6_array10 = mysql_fetch_array($link6_result10, MYSQLI_ASSOC);

        echo "You have $link6_rows10 items in your inventory."; <<---- returns 8

 

 

So why in the world does this not work??? What is the flaw in my logic?

 

$srchStr = null;

    foreach ($link6_array10 as $sn) {

    $srchStr .= " SerialNumber = '$sn' OR";

    echo "SN is $sn ";

    }

 

The only echo output of the foreach function is "SN is 12345" Not the 8 rows of content that I would expect. isn't this how foreach loops should work? Why do I not see "SN is 12345 SN is 23456 SN is 34567 etc..."????? I'm confused.

 

Thanks guys for any help.

 

the foreach loop is working just fine, the problem is your understanding of mysql_fetch_array.

 

mysql_fetch_array() retrieves a single row from the results set of the query and returns an array of the corresponding data in the fetched row, and moves the internal pointer ahead.

 

so in your code, the array $link6_array10 contains data from only one row of the results set, resulting in only one iteration of the specified foreach loop.

 

to get all of the data from your results set, you must iterate over each row grabbed from your query, this is typically done by a while loop or for loop (refer to stj's reply), although other methods can also be implemented.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.