acctman Posted November 3, 2008 Share Posted November 3, 2008 can someone help me with this foreach error [03-Nov-2008 10:58:57] PHP Warning: Invalid argument supplied for foreach() in /modules/blogs/blog.php on line 4 this is the only piece of code in the blog.php file if (!$en['user']) $en['user'] = $en['m_user']; $line = sql_fetch_assoc(sql_query("SELECT * FROM $membtable LEFT JOIN $picstable ON (i_user=m_id AND i_status=2) WHERE m_user='$en[user]'")); foreach ($line as $key => $value) $en['r'.$key] = $value; Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/ Share on other sites More sharing options...
bobbinsbro Posted November 3, 2008 Share Posted November 3, 2008 try adding a condition to check whether $line is empty before entering the foreach(): <?php if (count($line)){ //will return true when count returns != 0 foreach ($line as $key => $value) $en['r'.$key] = $value; } ?> Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681222 Share on other sites More sharing options...
acctman Posted November 3, 2008 Author Share Posted November 3, 2008 try adding a condition to check whether $line is empty before entering the foreach(): <?php if (count($line)){ //will return true when count returns != 0 foreach ($line as $key => $value) $en['r'.$key] = $value; } ?> still the same error Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681228 Share on other sites More sharing options...
bobbinsbro Posted November 3, 2008 Share Posted November 3, 2008 what does var_dump($line) return? Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681233 Share on other sites More sharing options...
acctman Posted November 3, 2008 Author Share Posted November 3, 2008 what does var_dump($line) return? dumps, all user member info in an array. from what I can see everything works fine the blog entry shows but the error log keeps getting the foreach() error Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681235 Share on other sites More sharing options...
kenrbnsn Posted November 3, 2008 Share Posted November 3, 2008 When trying to debug this, it's always better to do one statement per line and to add some error checks: <?php if (!$en['user']) $en['user'] = $en['m_user']; $q = "SELECT * FROM $membtable LEFT JOIN $picstable ON (i_user=m_id AND i_status=2) WHERE m_user='$en[user]'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); $line = mysql_fetch_assoc($rs); foreach ($line as $key => $value) $en['r'.$key] = $value; ?> Ken Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681238 Share on other sites More sharing options...
acctman Posted November 3, 2008 Author Share Posted November 3, 2008 still getting the same error, I even with the query selecting just m_id and m_user field only. this is odd become the blogs display correctly usernames, entries images, all show up, submitting and replying works too. When trying to debug this, it's always better to do one statement per line and to add some error checks: <?php if (!$en['user']) $en['user'] = $en['m_user']; $q = "SELECT * FROM $membtable LEFT JOIN $picstable ON (i_user=m_id AND i_status=2) WHERE m_user='$en[user]'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); $line = mysql_fetch_assoc($rs); foreach ($line as $key => $value) $en['r'.$key] = $value; ?> Ken Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681336 Share on other sites More sharing options...
kenrbnsn Posted November 3, 2008 Share Posted November 3, 2008 Put some debugging statements in: <?php if (!$en['user']) $en['user'] = $en['m_user']; $q = "SELECT * FROM $membtable LEFT JOIN $picstable ON (i_user=m_id AND i_status=2) WHERE m_user='$en[user]'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); if (mysql_num_rows == 0) echo 'Problem, no rows fetched with the query: ' . $q . '<br>'; else { $line = mysql_fetch_assoc($rs); if (!is_array($line)) echo 'Problem, $line is not an array<br>'; else { foreach ($line as $key => $value) $en['r'.$key] = $value; } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681347 Share on other sites More sharing options...
acctman Posted November 3, 2008 Author Share Posted November 3, 2008 this is the error i received after input the code below, Problem, no rows fetched with the query: SELECT * FROM rate_members LEFT JOIN rate_pictures ON (i_user=m_id AND i_status=2) WHERE m_user='testuser' Put some debugging statements in: <?php if (!$en['user']) $en['user'] = $en['m_user']; $q = "SELECT * FROM $membtable LEFT JOIN $picstable ON (i_user=m_id AND i_status=2) WHERE m_user='$en[user]'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); if (mysql_num_rows == 0) echo 'Problem, no rows fetched with the query: ' . $q . '<br>'; else { $line = mysql_fetch_assoc($rs); if (!is_array($line)) echo 'Problem, $line is not an array<br>'; else { foreach ($line as $key => $value) $en['r'.$key] = $value; } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681378 Share on other sites More sharing options...
rhodesa Posted November 3, 2008 Share Posted November 3, 2008 that if statement should be: if (mysql_num_rows($rs) == 0) Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681382 Share on other sites More sharing options...
kenrbnsn Posted November 3, 2008 Share Posted November 3, 2008 My goof. Sorry. Ken Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681384 Share on other sites More sharing options...
acctman Posted November 3, 2008 Author Share Posted November 3, 2008 I think that fixed, the foreach() error is now gone when run the new coding that kenrbnsn and rhodesa provided Link to comment https://forums.phpfreaks.com/topic/131214-foreach-error/#findComment-681437 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.