1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

"Call to a member function query() on a non-object" problem

Discussion in 'PHP' started by risoknop, Sep 6, 2008.

  1. #1
    The error I'm getting is:

    Here is the class casuing the problem. It's pretty simple, I have bolded the line 39 which is causing the problem:

    <?php
    
    /*
    Class: DbConnector
    Purpose: Connect to a database, MySQLi version
    */
    
    require_once 'SystemComponent.php';
    
    class DbConnector extends SystemComponent {
    
      private $mysqli;
    
      /*
      Function: __construct
      Purpose: Connect to the database
      */
      function __construct() {
        
        // Load settings from parent class
        $settings = parent::getSettings();
        
        // Get the main settings from array we just loaded
        $host = $settings['host'];
        $user = $settings['user'];
        $pass = $settings['pass'];
        $db = $settings['name'];
        
        // Connect to the database
        $this->mysqli = mysqli_init();
        $this->mysqli->real_connect($host, $user, $pass, $db);
    
      }
      
      /*
      Function: query
      Purpose: Execute a database query
      */
      function query($query) {
    
        return $this->mysqli->query($query);
      
      }
      
      /*
      Function: fetchArray
      Purpose: Get array of query results
      */
      function fetchArray($result) {
      
        return $result->fetch_array(MYSQLI_ASSOC); 
      
      }
      
      /*
      Function: close
      Purpose: Close the connection
      */
      function close() {
    
        $mysqli->close();
      
      }
      
    }
    
    ?>
    Code (markup):
    And here is the code:

    <?php
    
    require_once 'includes/DbConnector.php';
    
    $connector = new DbConnector();
    [B]$result = $connector->query("SELECT article FROM table");[/B]
    $connetor->fetchArray($result);
    
    ?>
    Code (markup):
    Anybody can help me solve this? Basically, I don't know why but $mysqli object is not available in query() function even though it is declared in class constructor.
     
    risoknop, Sep 6, 2008 IP
  2. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try

    
      function query($query) {
        global $mysqli;
        return $mysqli->query($query);
      
      }
    
    PHP:
    Regards,

    Steve


    EDIT: I believe this should also work, creating a class variable and accessing it through the class ($this)

    
    <?php
    
    /*
    Class: DbConnector
    Purpose: Connect to a database, MySQLi version
    */
    
    require_once 'SystemComponent.php';
    
    class DbConnector extends SystemComponent {
    
      var $myMySQLi = null;
    
      /*
      Function: __construct
      Purpose: Connect to the database
      */
      function __construct() {
        
        // Load settings from parent class
        $settings = parent::getSettings();
        
        // Get the main settings from array we just loaded
        $host = $settings['host'];
        $user = $settings['user'];
        $pass = $settings['pass'];
        $db = $settings['name'];
        
        // Connect to the database
        $this->$myMySQLi = mysqli_init();
        $this->$myMySQLi->real_connect($host, $user, $pass, $db);
    
      }
      
      /*
      Function: query
      Purpose: Execute a database query
      */
      function query($query) {
    
        return $this->$myMySQLi->query($query);
      
      }
      
      /*
      Function: fetchArray
      Purpose: Get array of query results
      */
      function fetchArray($result) {
      
        return $this->$myMySQLi->result->fetch_array();
      
      }
      
      /*
      Function: close
      Purpose: Close the connection
      */
      function close() {
    
        $this->$myMySQLi->close();
      
      }
      
    }
    
    ?>
    
    PHP:
    Untested, as I don't have access to PHP here, any problems let me know.
     
    Steve136, Sep 6, 2008 IP
  3. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nope that does nothing.

    I think the problem is the __construct() function hasn't been initiated. Don't know why because it's a constructor function so it should be processed every time new instance of the class is created.
     
    risoknop, Sep 6, 2008 IP
  4. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try the edited class above, try some echo's in the constructor before and after the mysqli code, then you know if it's being ran or not.

    Regards,

    Steve
     
    Steve136, Sep 6, 2008 IP
  5. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I have updated the class in the first post... query() function works now but fetchArray() doesn't. Error:

    I have bolded the problem line.
     
    risoknop, Sep 6, 2008 IP
  6. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    My fault,

    
    function fetchArray($result) 
    {      
       return $this->$myMySQLi->fetch_array($result);   
    }
    
    PHP:
     
    Steve136, Sep 6, 2008 IP
  7. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The problem is fetch_array() can only be used like this (which doesn't work):

    $result->fetch_array();
    Code (markup):
    According to PHP documentation - http://sk.php.net/manual/en/mysqli-result.fetch-array.php
     
    risoknop, Sep 6, 2008 IP
  8. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ahh, i thought it was the same as MySQL (silly me)

    Does this work?

    
    function fetchArray($result) {         
     return $result->fetch_array(MYSQLI_ASSOC);   
    }
    
    PHP:
    If not, then i'm not sure - Not used MySQLi before.

    Regards,

    Steve
     
    Steve136, Sep 6, 2008 IP
    risoknop likes this.
  9. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That's what I have there right now and I'm getting this error:

    Anyways, thanks for help :) Rep added

    I will play with it a bit, maybe I will find some solution.
     
    risoknop, Sep 6, 2008 IP
  10. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Anybody got any idea?

    I've been playing with the code and trying different approaches but still it isn't working :(

    It's easy with MySQL but with MySQLi it's problematic cause everything is object oriented...
     
    risoknop, Sep 6, 2008 IP
  11. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Ok. I have used var_dump($result); and found out that $result is bool(false) - so even DbConnector::query() metho doesn't work, even though there is no error...

    Anybody could copy the script and try it on you localhost?
     
    risoknop, Sep 6, 2008 IP
  12. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Problem solved ;)

    The table was called 'table' which is a so called MySQL reserved word. I have renamed the table and everything works :)
     
    risoknop, Sep 6, 2008 IP