Fatal error: Call to a member function num_rows() on a non-object
Results 1 to 6 of 6

Thread: Fatal error: Call to a member function num_rows() on a non-object

  1. #1
    Senior Member
    Join Date
    Mar 2008
    Location
    UK
    Posts
    201

    Question Fatal error: Call to a member function num_rows() on a non-object

    This is a complete n00b question but I cannot figure out what is wrong. As i have a class doing pretty much the same as what is happening in the error that's working :S

    Fatal error: Call to a member function num_rows() on a non-object.

    PHP Code:
    class basket
    {
    private 
    $items;
    private 
    $quantity;
    private 
    $database;
    private 
    $error;

    public static 
    $instance;

        private function 
    __construct()
            {
                
    //create an instance of the database class and connect to the database
                
    $this->database database::getinstance();
                
    //load the session items into the property $this->items
                
    $this->items $_SESSION['items'];
                
    //set the standard error
                
    $this->error 'Product Does Not Exist!';
            }

        public function 
    getinstance()
            {
                if(
    self::$instance === null)
                    {
                        
    $c __CLASS__;
                        
    self::$instance = new $c;
                    }
                return 
    self::$instance;
            }

        public function 
    add($id)
            {
                if (
    $this->items[$id] <= 0)
                    {
                        
    $this->items[$id] = 0;
                    }
                if (
    ereg("([A-Z0-9]+)"$id))
                    {
                        
    $results $this->database->query("SELECT * FROM `products` WHERE code='$id'");
                        
    //line 52 - Fatal error: Call to a member function num_rows() on a non-object
                        
    if ($results->num_rows() == 1)
                            {
                                
    $this->items[$id]++;
                            }
                        else
                            {
                                return 
    $this->error;
                            }
                    }
                else
                    {
                        return 
    $this->error;
                    }
            }

        private function 
    __destruct()
            {
                
    $_SESSION['items'] = $this->items;
            } 
    //note: I didn't include all the class as it just hinders the problem

  2. #2
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,460
    It sounds like the database connection object was not created.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  3. #3
    Senior Member
    Join Date
    Mar 2008
    Location
    UK
    Posts
    201
    If the DB object wasn't created surly the $this->database->query() would error first :S

    It seems that I was trying to call num_rows as a function rather than a property.\

    PHP Code:
    public function add($id)
            {
                if (
    $this->items[$id] <= 0)
                    {
                        
    $this->items[$id] = 0;
                    }
                if (
    ereg("([A-Z0-9]+)"$id))
                    {
                        if (
    $results $this->database->query("SELECT * FROM `products` WHERE code='$id'"));
                            {
                                if (
    $results->num_rows == 1)
                                    {
                                        
    $this->items[$id]++;
                                        
    $results->close();
                                        return 
    1;
                                    }
                                else
                                    {
                                        return 
    $this->error;
                                    }
                            }
                    }
                else
                    {
                        return 
    $this->error;
                    }
            } 
    This seems to work now.

    Anyone know what causes these errors:
    Warning: Call to private basket::__destruct() from context '' during shutdown ignored in Unknown on line 0

    Warning: Call to private database::__destruct() from context '' during shutdown ignored in Unknown on line 0
    Last edited by knowj; 03-24-2008 at 11:47 AM.

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,820
    I have not seen it explicitly stated anywhere yet, but it appears that the destructor must have public visibility, thus the error since you defined your __destruct() as private.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  5. #5
    Senior Member
    Join Date
    Mar 2008
    Location
    UK
    Posts
    201
    Sorry I should have updates this when I sorted it.

    Your right nogdog destructor's need to be public. The logic in that I cannot understand.

    mysqli seems to be pretty poorly documented in some areas as well from the research I have been doing. Some functions sound like there going to be really useful but when you look into them they have no explanation.

  6. #6
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,460
    Your right nogdog destructor's need to be public. The logic in that I cannot understand.
    If it is non-public, the object cannot be destroyed (unless it is protected and invoked from a derived class scope).
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •