Jump to content

Archived

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

kutuitem

Invalid argument supplied for foreach()

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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!!!!

Share this post


Link to post
Share on other sites

×

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.