kutuitem
-
Posts
2 -
Joined
-
Last visited
Never
Posts posted by kutuitem
-
-
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?
Invalid argument supplied for foreach()
in PHP Coding Help
Posted
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!!!!