#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0

    Call to member function fetch_array on non-object.


    Alright, this isn't really an error that's fatal but more of one that's annoying... My code is working perfectly fine, however i keep getting this error: Fatal error: Call to a member function fetch_array() on a non-object in C:\xampp\htdocs\includes\class\user.php on line 51

    The code goes through, my SQL is inserted, updated, etc etc into the database, but the error persists...

    PHP Code:
    <?php
    class user extends mysqli
    {
        function 
    __construct()
        {
            
    $this->connect(DB_HOSTDB_USERDB_PASS);
            
    $this->select_DB('thc_primary');    
        }
        function 
    setSession($username$password)
        {
                
    $username $this->real_escape_string($username);
                
    $password md5($password);
                
    $sql "SELECT username, password, uid FROM users WHERE username = '{$username}' AND password = '{$password}';";
                
    $que $this->query($sql);
                if(
    $que)
                {
                    while(
    $row $que->fetch_array())
                    {
                        
    $_SESSION['uid'] = $row[2];
                    }
                }
        }
        function 
    registerUser($username$password$email$citcode)
        {
            
    $username $this->real_escape_string($username);
            
    $password md5($password);
            
    $email $this->real_escape_string($email);
            
    $date date('Y-m-d');
            
    //* Checks to see if the user name and email are in use
            
    $sql "SELECT count(username), count(email) FROM users WHERE username = '".$username."'";
            
    $que $this->query($sql);
            while(
    $row $que->fetch_array())
            {
                if(
    $row[0] > || $row[1] > 0)
                {
                    
    //* If UN or EM are in use, return the user to previous page, this should be altered later to show an error
                    
    echo "Failure: Username / Email exists";    
                }
                else
                {
                    
    //* check to see if the 'citizen code' exists, checks the number of active codes against the max number of citizen codes avail.
                    
    $sql "SELECT citizen_code, amount, active FROM citizen_codes WHERE citizen_code = '{$citcode}';";
                    
    $que $this->query($sql);
                    if(!
    $que)
                    {
                        
    //* return to sender
                        
    die("Error! Citizen Code Error".mysqli_error($this));
                    }
                    else
                    {
                        while(
    $row $que->fetch_array())
                        {
                            if(
    $row[1] > $row[2])
                            {
                                
    $active $row[2]+1;
                                
    $sql "UPDATE citizen_codes SET active = '{$active}' WHERE citizen_code = '{$citcode}';";     
                                
    $sql .= "INSERT INTO users(username, password, email, joindate) VALUES ('{$username}','{$password}', '{$email}', '{$date}');";
                                
    $que $this->multi_query($sql);
                                if(!
    $que)
                                {
                                die(
    'could not register!'.mysqli_error($this));
                                }
                                else
                                {
                                echo 
    "WINNER!";
                                }
                            }    
                        }
                    }
                }
            }
        }
    }
  2. #2
  3. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Hrm. I'm not sure how your description could be true.

    If that error is being encountered then there's no way your script could have looped through the results of that last SELECT in order to execute the INSERT and UPDATE within it.

    And the !$que check should prevent line 51 from even being executed to begin with.

    Things just aren't adding up here.

    Other notes:
    - It's advisable to use a password encryption technique better than md5. I think we have topics about that in the FAQ area.
    - It's something of an odd practice for a class like this to extend mysqli. It'd be more conventional to simply have a database handler variable on the class that's set within __construct()
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0
    Originally Posted by dmittner
    Hrm. I'm not sure how your description could be true.

    If that error is being encountered then there's no way your script could have looped through the results of that last SELECT in order to execute the INSERT and UPDATE within it.

    And the !$que check should prevent line 51 from even being executed to begin with.

    Things just aren't adding up here.
    Hence the reason i posted it here, it doesn't make sense at all.. I mean, its not really a big deal. the code is working fine, I can just suppress the error since its not really effecting anything at all. but I figured someone with a bigger tenure with PHP could point out what's wrong with it...

    Other notes:
    - It's advisable to use a password encryption technique better than md5. I think we have topics about that in the FAQ area.
    - It's something of an odd practice for a class like this to extend mysqli. It'd be more conventional to simply have a database handler variable on the class that's set within __construct()
    The password encryption and everything is just placeholder right now, i'm getting back into the swing of PHP, it's been a couple years...
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    it's the $que = $this->multi_query($sql); in line 58 that causes the problem. Since multi_query() always returns a boolean, you run into trouble next time you try to fetch from $que.

    What do we learn from that? Generic variables are evil if used in a big scope. You redefine $que three(!) times. This can't be good.

    Comments on this post

    • dmittner agrees : Ah. Dangit. Missed that.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    D'oh. Yep. What Jacque's said.

    Well, more specifically it's because you're using the $que variable at 3 different levels of query nesting. Each use is overwriting the previous so, as Jacques said, your inner-most use is overwriting and causing the error on your second loop.

    Quick fix would be to use $que1, $que2, and $que3 at each respective level.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0
    ah, alright. fixed the problem... and now i just have one question, seeing as dmittner brought it up...
    - It's something of an odd practice for a class like this to extend mysqli. It'd be more conventional to simply have a database handler variable on the class that's set within __construct()
    could someone elaborate on this for me, please?
  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    could someone elaborate on this for me, please?
    MySQLi is a database connection class. A "user" isn't a logical extension of a database connection. A more logical structure would be to have two classes, one for your database connection and one for your user. When you create a user, you pass it an instance of the database class and the user object uses the instance of the database class to run queries.

    Plus, presumably you will store more than just users in your database and your app may construct multiple user objects (or other objects) and it would not be a good idea to open a new database connection for every object that your application instantiates. You only need one database connection for all of the objects to share.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Burb of Detroit, Michigan
    Posts
    92
    Rep Power
    78
    In the MVC approach the Database connection class would typically be part of the Controller and the User class would typically be part of the Model (data).
    Last edited by Strider64; July 11th, 2013 at 07:40 AM.
  16. #9
  17. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by Strider64
    In the MVC approach the Database connection class would typically be part of the Controller and the User class would typically be part of the Model (data).
    Hmm, maybe just a semantics misunderstanding on my part, but wouldn't the database connection class simply be instantiated/configured from the Controller, but with the class itself being part of the Model?

    And that'd just be assuming you're using injection, which I'm not sure is a prerequisite of MVC? I'm not even sure how I'd do connection injection in the multiton pattern of my system...
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0
    Originally Posted by E-Oreo
    MySQLi is a database connection class. A "user" isn't a logical extension of a database connection. A more logical structure would be to have two classes, one for your database connection and one for your user. When you create a user, you pass it an instance of the database class and the user object uses the instance of the database class to run queries.

    Plus, presumably you will store more than just users in your database and your app may construct multiple user objects (or other objects) and it would not be a good idea to open a new database connection for every object that your application instantiates. You only need one database connection for all of the objects to share.
    Alright, I understand the concept of what you are saying, however I do not understand the application. I get how it would be good but have no idea how to implement it. Could you possibly direct me towards a tutorial or example of this?
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Originally Posted by dmittner
    Hmm, maybe just a semantics misunderstanding on my part, but wouldn't the database connection class simply be instantiated/configured from the Controller, but with the class itself being part of the Model?

    And that'd just be assuming you're using injection, which I'm not sure is a prerequisite of MVC? I'm not even sure how I'd do connection injection in the multiton pattern of my system...
    The database IS the Model (usually), and therefore the db connection is solely the domain of the Model bit of your code. The most distinct MVC I've ever used had the model classes on a completely different server. The controllers used SOAP calls to do all their CRUD operations.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  22. #12
  23. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by ManiacDan
    The database IS the Model (usually), and therefore the db connection is solely the domain of the Model bit of your code. The most distinct MVC I've ever used had the model classes on a completely different server. The controllers used SOAP calls to do all their CRUD operations.
    True, but isn't the point of dependency injection that the code that instantiates model objects, provides those objects with things like the database connector?

    That way your unit testing code can instead send a mock connector rather than a real one, to test your object's interaction with it.

    At least that's how I've interpreted it when I've seen DI pushed as a unit testing-enabling model.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0
    Seeing as my question got glazed over (mostly because i posted at the same time as some one else) where could i find tutorials on MCV?

IMN logo majestic logo threadwatch logo seochat tools logo