Click here to Skip to main content
14,695,439 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, im so tired to find any solutions and debug about my project, i dont know whats wrong about it. can someone help me for this :(


This is my DB.php that holds database functions.

public static function getInstance(){
        if(!isset(self::$_instance)){
            self::$_instance = new DB();
        }
        return self::$_instance;
    }

    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->_result = $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);

    }


    //checking for error in database
    public function error(){
        return $this->_error;
    }



and this is my index.php that test if the function action get are return.
but im having a " Call to a member function error() on a non-object in C:\wamp\www\ooplr\index.php on line 11" in "$user"



$user = DB::getInstance()->get('users', array('username', '=', 'alex'));

if($user->error()){
echo 'No User';
}else{
echo 'Okay!';
}
Posted
Updated 9-Feb-15 8:22am
v2
Comments
Richard MacCutchan 9-Feb-15 11:20am
   
The message seems reasonably clear; you are trying to call error on something that is not an object.
Member 11111612 9-Feb-15 12:41pm
   
to be honest im just a newbie. that codes is in the tutorial. i read and search about he's codes but i never solve the error

1 solution

Well we were all newbies at one time. But the best lesson most of us learned was to study the documentation. That should help you to understand what type of result is returned from the call to
DB::getInstance()->get('users', array('username', '=', 'alex'));

Also, the code that has the error does not appear to be in the block you have posted so the context is difficult to guess, but I can only assume that $user is some simple type.
   
Comments
Member 11111612 9-Feb-15 13:12pm
   
if(!$this->query($sql, array($value))->error()){
return $this;

}
}
}
return false; // gets error i dont know why.
Member 11111612 9-Feb-15 13:13pm
   
Yep, bro. DB::getInstance()->get('users', array('username', '=', 'alex')); is selecting all data in database which is alex. but "DB::getInstance()->get('users', array('username', '=', 'alex'));" has an error, but i really sure that codes is right? i dont know any angle whats wrong about it.
Richard MacCutchan 9-Feb-15 13:17pm
   
Well it is obviously not right, otherwise it would not give an error. I suggest you edit your original question and remove the code block that you entered, and replace it with the actual code that produces the error, and the exact error message. Also make sure that you indicate clearly which line the error occurs on.
Member 11111612 9-Feb-15 13:18pm
   
Okay, i will. btw thanks for suggestions.
Richard MacCutchan 9-Feb-15 14:44pm
   
Looking at this again (I'm no PHP expert) it appears that you have declared a local function called error, but you are trying to call it as a member of the variable $user. But error is not connected to that variable, so the code makes no sense. You have to use some other mechanism to determine if the varaible is in some way invalid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900