Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cxsey
    New Member
    • Mar 2010
    • 5

    Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource

    I get the following error code using this code:
    "Warning: mysql_numrows() : supplied argument is not a valid MySQL result resource"

    It's on line 55 in this php script:

    Code:
    <?php
    /**
     * Database.php
     * 
     * The Database class is meant to simplify the task of accessing
     * information from the website's database.
     *
     * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
     * Last Updated: August 17, 2004
     */
    include("constants.php");
          
    class MySQLDB
    {
       var $connection;         //The MySQL database connection
       var $num_active_users;   //Number of active users viewing site
       var $num_active_guests;  //Number of active guests viewing site
       var $num_members;        //Number of signed-up users
       /* Note: call getNumMembers() to access $num_members! */
    
       /* Class constructor */
       function MySQLDB(){
          /* Make connection to database */
          $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
          mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
          
          /**
           * Only query database to find out number of members
           * when getNumMembers() is called for the first time,
           * until then, default value set.
           */
          $this->num_members = -1;
          
          if(TRACK_VISITORS){
             /* Calculate number of users at site */
             $this->calcNumActiveUsers();
          
             /* Calculate number of guests at site */
             $this->calcNumActiveGuests();
          }
       }
    
      
    
    . . . . 
    
    /**
        * calcNumActiveUsers - Finds out how many active users
        * are viewing site and sets class variable accordingly.
        */
       function calcNumActiveUsers(){
          /* Calculate number of users at site */
          $q = "SELECT * FROM ".TBL_ACTIVE_USERS;
          $result = mysql_query($q, $this->connection);
          $this->num_active_users = mysql_numrows($result);
       }
     
    
    . . . .
       
       /**
        * query - Performs the given query on the database and
        * returns the result, which may be false, true or a
        * resource identifier.
        */
       function query($query){
          return mysql_query($query, $this->connection);
       }
    };
    
    /* Create database connection */
    $database = new MySQLDB;
    
    ?>
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2432

    #2
    I would first diagnose the query.

    below line 53 add the following

    echo "$q";
    exit;

    Then see if there is anything wrong with the query itself. You can paste the output here, we might be able to help with any syntax issues.
    niheel @ bytes

    Comment

    • Cxsey
      New Member
      • Mar 2010
      • 5

      #3
      This is what appeared:

      SELECT * FROM active_users

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hey.

        I explain the reasons for this error in detail in this article. Maybe that will help.

        Comment

        • Cxsey
          New Member
          • Mar 2010
          • 5

          #5
          I don't really understand that.

          Can you just show me how to do it through my code?

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Put simply, this error is showing because the mysql_query call preceeding the mysql_num_rows call is failing. Thus, the result of the query call is not a valid resources, which causes the num_rows function to fail and produce this error.

            What you need to do is verify that the query call is successful before you try to use it's return value, and if it is not valid, print an error message detailing why it failed. - See the last two examples in the article. See how I use the two functions. - You need to do something similar to your code so that it can handle running into this problem without the code failing with this fairly uninformative error message.

            A simple trick to accomplish this is to add a " or die(mysql_error ());" to your mysql_query call. (See my last example.) That halts the execution of the code if the query fails and shows an error message telling you why.

            Comment

            • Cxsey
              New Member
              • Mar 2010
              • 5

              #7
              k. now it says Table 'a2161410_login .active_users' doesn't exist

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Does it exist?
                If not, you will need to change the query so that it uses the correct table name. That, in your case, requires you to alter the "TBL_ACTIVE_USE RS" constant. (In your "constants. php" file, I'm guessing.)

                Comment

                • Cxsey
                  New Member
                  • Mar 2010
                  • 5

                  #9
                  Ahh. Yes.

                  In the constants file, this is shown.
                  Code:
                  define("TBL_USERS", "users");
                  define("TBL_ACTIVE_USERS",  "active_users");
                  define("TBL_ACTIVE_GUESTS", "active_guests");
                  define("TBL_BANNED_USERS",  "banned_users");
                  define("TBL_MAIL", "mail");
                  Isn't it the same a in the database file?
                  Last edited by Atli; Mar 20 '10, 11:02 AM. Reason: Added [code] tags.

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    By including the constants file into the database file, everything in the constants file is imported into the database file and executed as part of it. - You can think of it like the equivalent of copying everything from the constants file and pasting it over the include statement in the database file.

                    The DB_NAME constant is also important to your error. It defines the name of the database you are using, so you need to find that and make sure it has the correct value.

                    Comment

                    Working...