Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts

    "Call to a member function on a non-object" while on object

    I got a problem when trying to check if a query is executed. While calling it straight away by my query function it does work. However when doing it by the get function, which does nothing more then simplify the query function usage it doesn't work anymore.

    Error message: Fatal error: Call to a member function error() on a non-object
    index
    PHP Code:
    <?php
    $user 
    DB::getInstance()->get('users', array('username''=''Tom'));
    //$user = DB::getInstance()->query("SELECT username FROM users WHERE username = ?", array('Tom'));

    if($user->error()) {
        echo 
    'No user';
    } else {
        echo 
    'Ok';
    }
    db class
    PHP Code:
    public function query($sql$params = array()) {
            
    $this->_error false;
            if(
    $this->_query $this->_pdo->prepare($sql)) {
                
    $x 1;
                if(
    count($params)) {
                    foreach(
    $params as $param) {
                        
    $this->_query->bindValue($x$param);
                        
    $x++;
                    }
                }

                if(
    $this->_query->execute()) {
                    
    $this->_results $this->_query->fetchAll(PDO::FETCH_OBJ);
                    
    $this->_count $this->_query->rowCount();
                } else {
                    
    $this->_error true;
                }
            }

            return 
    $this;
        }

        public function 
    action($action$table$where = array()) {
            if(
    count($where) == 3) {
                
    $operators = array('=''>''<''>=''<=');

                
    $field '$where[0]';
                
    $operator '$where[1]';
                
    $value '$where[2]';

                if(
    in_array($operator$operators)) {
                    
    $sql "{$action} FROM {$table} WHERE {$field} {$operator} ?";

                    if(!
    $this->query($sql, array($value))->error()) {
                        return 
    $this;
                    }
                }
            }

            return 
    false;
        }

        public function 
    get($table$where) {
            return 
    $this->action('SELECT *'$table$where);
        }

        public function 
    delete($table$where) {
            return 
    $this->action('DELETE'$table$where);
        }

        public function 
    error() {
            return 
    $this->_error;
        } 
    Thanks in advance.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Solved it this can be locked. Used quotes when defining field, operator and value when I shouldn't. And could've fixed another thing or two.


  •  

    Posting Permissions

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