SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Call to a non-object error

    Hey,

    I created a method to allow me to pull the first 5 results in my database and display them.

    PHP Code:
      public function storeFront()
      {
        
    $mysqli = new mysqli($this->host$this->user$this->password$this->database);
        if (
    mysqli_connect_error())
          Throw New 
    Exception('Error: ' .mysqli_connect_error());
        
        
    $result $mysqli->query("SELECT id, name, description, price, quantities 
                                  FROM products 
                                  LIMIT 5"
    );
        
        echo 
    '<div id="newest">';
        while (
    $row $result->fetch_object())
        {
          echo 
    '<div class="block">'
                  
    .'<b>Name: </b>' .$row->name'<br />'
                  
    .'<b>Description: </b>' .$row->description'<br />'
                  
    .'<b>Price: </b>'.$row->price.'<br />'
                  
    .'<b>Quantities: </b>' .$row->quantities'<br />'
                
    .'</div>';
        }
        echo 
    '</div>';
      }

    I'm getting an error:

    Fatal error: Call to a member function fetch_object() on a non-object in C:\Users\9three\Desktop\Server\htdocs\cart\package\cart.php on line 28

    line 28 is: while ($row = $result->fetch_object())

    fetch_object() is the correct method and I seem to be passing it correct. Does anyone see where I went wrong?

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The query is not returning an object. There is something wrong with it. Try:
    PHP Code:
        $result $mysqli->query("SELECT id, name, description, price, quantities 
                                    FROM products 
                                   LIMIT 5"
    );
        
    var_dump($mysqli->error); 

  3. #3
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, it's out putting "no database selected"

    But i'm selecting my database within my constructor

    PHP Code:
    public function __construct($host ''$user ''$password ''$database '')
      {
        
    $this->host $host;
        
    $this->user $user;
        
    $this->password $password;
        
    $this->database $databse;
      } 
    I initialize it like so:

    PHP Code:
    $cart = new ShoppingCart('localhost''root''''store'); 

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
      public function storeFront()
      {
        
    var_dump($this->database);
        
    $mysqli = new mysqli($this->host$this->user$this->password$this->database); 

  5. #5
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $this->database $databse
    Should this be?:
    PHP Code:
    $this->database $database
    "Don't you just love it when you solve a programming bug only to create another."

  6. #6
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice catch Spence.

    freaking typos

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you code with error_reporting set to E_ALL, typos like that would have cost you but mere seconds.


Bookmarks

Posting Permissions

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