I Have this error code coming up on one of my php files.

<b>Warning</b>: Invalid argument supplied for foreach() in <b>/home/user/public_html/userupdate.php</b> on line <b>35</b><br />

$query_checkRef = mysql_query("SELECT referral_ID from members WHERE username= '".$subid."'") or die(mysql_error());
foreach(mysql_fetch_array($query_checkRef) as $ref_id_user);
if ($ref_id_user>=1)

The one in question is the foreach(mysql) line....

How can I fix this?😕

    i would suggest dropping the foreach() in this case for the standard practice of using while(). the manual entry for mysql_fetch_array() has more examples

    while ($row = mysql_fetch_array($query_checkRef, MYSQL_ASSOC)) {
    ...
      dagon;10985014 wrote:

      i would suggest dropping the foreach() in this case for the standard practice of using while(). the manual entry for mysql_fetch_array() has more examples

      while ($row = mysql_fetch_array($query_checkRef, MYSQL_ASSOC)) {
      ...

      So in line 35 I should replace with while ($row = mysql_fetch_array($query_checkRef, MYSQL_ASSOC)) { ?

        are you expecting more than one id? if not there's no need to fetch an array at all.

          dagon;10985019 wrote:

          are you expecting more than one id? if not there's no need to fetch an array at all.

          Yes there will be more than one ID.

          Also when I added this code, now I get, syntax error, unexpected T_AS in on line 35.

          Does that mean that the php.ini file needs to be edited? I heard usually errors like that refer to the php.ini file.

            what's the code you have now?

              $query_checkRef = mysql_query("SELECT referral_ID from members WHERE username= '".$subid."'") or die(mysql_error());
              //foreach(mysql_fetch_array($query_checkRef) as $ref_id_user);
              while ($row = mysql_fetch_array($query_checkRef, MYSQL_ASSOC)) as $ref_id_user);
              if ($ref_id_user>=1)

              Lines 34 through 37

              I commented out the original code so I didn't lose it replacing it with what you told me to use.

                look at the code i posted again, and the examples on the manual page i pointed you to.

                  I'm still not getting it. I'm looking at the manual page but I guess not proficient enough in php to understand it.

                    Duh,

                    I got it.

                    Had to remove the as $ref_id_user).

                    Thanks!

                      Write a Reply...