stj5353 Posted February 3, 2012 Share Posted February 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256312-why-is-my-foreach-loop-not-working-with-this-array/ Share on other sites More sharing options...
shaiang Posted February 3, 2012 Share Posted February 3, 2012 Try something like this: $resultArray = array(); $result = mysql_query($link6_sql10) or die("Link6 SQL10 Failed: Function drawInventory: " . mysql_error()); while( $row = mysql_fetch_array($result)){ $resultArray[] = $row; } Quote Link to comment https://forums.phpfreaks.com/topic/256312-why-is-my-foreach-loop-not-working-with-this-array/#findComment-1313943 Share on other sites More sharing options...
stj5353 Posted February 3, 2012 Author Share Posted February 3, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/256312-why-is-my-foreach-loop-not-working-with-this-array/#findComment-1314050 Share on other sites More sharing options...
AyKay47 Posted February 3, 2012 Share Posted February 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256312-why-is-my-foreach-loop-not-working-with-this-array/#findComment-1314056 Share on other sites More sharing options...
stj5353 Posted February 3, 2012 Author Share Posted February 3, 2012 Oh... I was misunderstanding fetch_array(). Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/256312-why-is-my-foreach-loop-not-working-with-this-array/#findComment-1314059 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.