Jump to content

Foreach Error


acctman

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.