Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got a problem

I'm new to PDO, so i'm trying to get all data from a tabla, very easy i know, but $this variable is giving me some problem, i wrote a class called DB to connect to mysql, and i use it in another class called getAll with a function called get;

DB class

<?php 
class DB
{
    private $_dbh;
    private static $_instance;
    private function __construct()
    {
        try 
        {
            $this->_dbh = new PDO("mysql:host=127.0.0.1;dbname=telmex","root","a.d.s.l");                   
        } 
        catch (PDOException $e) 
        {
            die($e->getMessage());  
        }
    }

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

        return self::$_instance;    
    }
}

?>

getAll Class

<?php 

class getAll
{
    public $_dbh;
    public function __construct()
    {
        $this->_dbh=DB::Link();
    }

    public static function get($tabla)
    {
        $sql="SELECT * FROM ".$tabla."";
        if($sentencia=$this->_dbh->prepare($sql))
        {
            echo "Prepared.";
        }   
    }
} ?>

So when i run

<?php 
$select = new getAll();
    $consultar = $select->get("users");
 ?>

it gives me a fatal error.

share|improve this question
3  
remove the static from the definition of public static function get($tabla) because you're not calling it statically, you're treating it as an instance method –  Mark Baker Mar 28 at 17:42
1  
possible duplicate of Fatal Error For $this variable –  Amal Murali Mar 28 at 17:45
    
Still giving the same error –  DaveSanchez Mar 28 at 17:53
add comment

1 Answer 1

remove this line from getAll class public $_dbh; and make it extend of DB class

share|improve this answer
1  
Except there won't be a valid database connection unless you make a few other changes as well –  Mark Baker Mar 28 at 17:47
    
still having the same problem, and the connection works well –  DaveSanchez Mar 28 at 17:50
    
can you write exactly what is the problem from apache log ? –  Anri Mar 28 at 17:53
    
Fatal error: Using $this when not in object context in /var/www/html/PDO/Clases/getAll.php on line 14 –  DaveSanchez Mar 28 at 17:55
    
remove this line from getAll class public $_dbh; and make it extend of DB class –  Anri Mar 28 at 17:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.