kutuitem Posted March 13, 2006 Share Posted March 13, 2006 Hi All,I get this warning (Invalid argument supplied for foreach()). Following is my code: foreach (sql_get('vms_enrollments', 'student_id', $_SESSION['id']) as $k1 => $v1) { $db = new DbConn();[b] foreach (sql_get('vms_tests', 'tr_mod_id', $v1['tr_mod_id']) as $k2 => $v2) <-- this line triggers the error[/b] { $sql = "select count(*) ". " from vms_test_results". " where test_id = ".$v2['test_id']. " and student_id = ".$_SESSION['id']; $res = $db->query($sql) or die('Could not execute:<br /><br />'.$sql); $row = mysql_fetch_row($res); if ($row[0] <= 0) $tests[] = $v2; } }If I change $v1['tr_mod_id'] to a number. Then no warning is issued. I'm really confused. Is this PHP bug? Quote Link to comment https://forums.phpfreaks.com/topic/4815-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
deonh Posted March 13, 2006 Share Posted March 13, 2006 foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.So make sure the function sql_get returns an array. Quote Link to comment https://forums.phpfreaks.com/topic/4815-invalid-argument-supplied-for-foreach/#findComment-16920 Share on other sites More sharing options...
kutuitem Posted March 13, 2006 Author Share Posted March 13, 2006 [!--quoteo(post=354436:date=Mar 13 2006, 05:27 PM:name=deonh)--][div class=\'quotetop\']QUOTE(deonh @ Mar 13 2006, 05:27 PM) [snapback]354436[/snapback][/div][div class=\'quotemain\'][!--quotec--]foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.So make sure the function sql_get returns an array.[/quote]Thx for the response. This is the result of print_r on sql_get:Array ( [0] => Array ( [test_id] => 1 [test_number] => 210 [test_topic] => Marketing 2 [test_name] => Marketing Sem2 2006 [test_data] => ##M::asdf::b::bb::aa::dfdff##E::ho ho ho ho [test_date] => 1141029115 [tr_mod_id] => 1 ) [1] => Array ( [test_id] => 2 [test_number] => 1212 [test_topic] => 1212 [test_name] => 121212 [test_data] => ##E::asdfasf##E::asdfdasfsafsadf [test_date] => 1141116140 [tr_mod_id] => 1 ) )Isn't this a valid array? This is really strange.... Please note that if I change v1['tr_mod_id'] to 1 or any number, I get no warning!!!! Quote Link to comment https://forums.phpfreaks.com/topic/4815-invalid-argument-supplied-for-foreach/#findComment-17034 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.