FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help me find the error in my code... mysql_fetch_array():





Ecthelion
Hi,

As you might have guessed, I'v written some code and I have problems with it.
I've written a bot-recognising page on my forum, which recognises bots and alerts when there are non-bot people who need to be set the right permissions.
Giving the permissions is done by checking the box right of their name and say add permissions.

The bot recognising, removal, ... everything works fine, except adding the right permissions to the non-bot users.
This is done by adding them to a group on the forum which will allow them to post on all parts of the forum.

But somehow I get an error there...
Quote:
Warning: mysql_fetch_array(): 22 is not a valid MySQL result resource in /home/ecthelio/domains/teamfsn.frih.net/public_html/phpBB2/removebot.php on line 71


This is very strange, because the line it points to is indeed a line with mysql_fetch_array(), but a bit further in my code, I use the exact same code again, with the same mysql request, and it doesn't give an error there.

Doesn someone knows how to solve this error, or can someone find the error in my code?
Thanks in advance.


Code:
if($userdata['user_level'] == ADMIN){
   $sql = "SELECT u.user_id , u.username, u.user_posts FROM phpbb_users u WHERE u.user_active =1 AND u.user_lang = 'french' AND u.user_id <>-1";
   if ( !($result = $db->sql_query($sql)) ){
      message_die(GENERAL_ERROR, 'Error getting group information', '', __LINE__, __FILE__, $sql);
   }
   //Then see if we have to add someone to the non-bot list
   while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {  // <<<===== The error points to this line
      $sql2 = "SELECT u.user_id, u.username
      FROM phpbb_users u, phpbb_user_group g
      WHERE g.group_id = 32
      AND u.user_id = " . $line['user_id'] . "
      AND g.user_id = u.user_id";
      if ( !($result2 = $db->sql_query($sql2)) )
      {
         message_die(GENERAL_ERROR, 'Error getting group information', '', __LINE__, __FILE__, $sql);
      }
      if($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)){
         //The user is in the group, do nothing   
      }else{
         //Did the user post anything?
         if($line['user_posts'] != 0){
            //The user was not found in the group, so print the result an set toactivate on 1
            $toactivate = 1;
            //Do we have to activate him?
            $toadd = "add_" . $line['user_id'];
            //echo $line['user_id'];
            if(isset($HTTP_GET_VARS[$toadd])){
               ?>
               <table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0" background="templates/aqua/images/aquatbm.gif">
               <tr align="center" background="templates/aqua/images/aquatbm.gif">
               <td width="33%" height="22" align="left"><img src="templates/aqua/images/aquatbl.gif" width="22" height="22"></td>
               <td width="33%" height="22" align="center"><span class="cattitle">&nbsp;&nbsp;</span></td>
               <td width="33%" height="22" align="right"><img src="templates/aqua/images/aquatbr.gif" width="79" height="22"></td>
               </tr>
               </table>
               <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="forumline">
               <tr>
               <th class="thHead" height="25" align="center"><span class="tableTitle"> &nbsp;Vous avez ajouté <? echo $line['username'] ?>aux groupe 'Vivants'!&nbsp;</span></th><th class="thHead" height="25" align="left"></th>
               </tr>
               <table width="90%" height="22"  border="0" align="center" cellpadding="0" cellspacing="0" background="templates/aqua/images/aquatbdm.gif">
               <tr>
               <td><img src="templates/aqua/images/aquatbdl.gif"></td>
               <td align="right"><img src="templates/aqua/images/aquatbdr.gif"></td>
               </tr></table></br>
               <?
               //And now add him for real
               $sql3 = "INSERT INTO `phpbb_user_group` (group_id, user_id, user_pending) VALUES(32," . $line['user_id'] . " ,0)";
               $db->sql_query($sql3);
            }//End isset
         }//end number of post
         $db->sql_freeresult($result2);
      }//end if in group
      $db->sql_freeresult($result);
   }//end while
}//end userlevel
vinx_18
Code:
$sql = "SELECT u.user_id , u.username, u.user_posts FROM phpbb_users u WHERE u.user_active =1 AND u.user_lang = 'french' AND u.user_id <>-1";


Check that line.. there might be inconsistencies between the code and the database fields.
Ecthelion
When I enter
"SELECT u.user_id , u.username, u.user_posts FROM phpbb_users u WHERE u.user_active =1 AND u.user_lang = 'french' AND u.user_id <>-1"
in phpMyAdmin I get the result I want...
So it's not that...
fromegame
Is $db->sql_query($sql) returning a value? I think you defined the class-function this:
Code:
function sql_query($sql) {
        mysql_query($sql);
}


Change it in:

Code:
function sql_query($sql) {
        return mysql_query($sql);
}
Ecthelion
Hi, thanks for the reply.

The function sql_query is the function defined in phpBB, since this is a page that I add to my forum, I use the functions of the forum, whitout having to rewrite them.

Anyway, I found the error.
My code was correct, until
Code:
      }//end if in group
      $db->sql_freeresult($result);
   }//end while
}//end userlevel


The problem was that I do a freeresult in the while, and the while uses the rsult to see if it has to continue.

Changing my code to
Code:
      }//end if in group
   }//end while
   $db->sql_freeresult($result);
}//end userlevel

Solved my problem, and everything works fine now.

Anyway, thanks for the help!
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.