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; Quote Link to comment 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; } ?> Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted November 3, 2008 Share Posted November 3, 2008 what does var_dump($line) return? Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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) Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 3, 2008 Share Posted November 3, 2008 My goof. Sorry. Ken Quote Link to comment 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 Quote Link to comment 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.