0

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.

3
  • 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 Commented Mar 28, 2014 at 17:42
  • 1
    possible duplicate of Fatal Error For $this variable Commented Mar 28, 2014 at 17:45
  • Still giving the same error Commented Mar 28, 2014 at 17:53

3 Answers 3

0

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

Sign up to request clarification or add additional context in comments.

4 Comments

Except there won't be a valid database connection unless you make a few other changes as well
can you write exactly what is the problem from apache log ?
Fatal error: Using $this when not in object context in /var/www/html/PDO/Clases/getAll.php on line 14
remove this line from getAll class public $_dbh; and make it extend of DB class
0
<?php 
class getAll extends DB {
    public function __construct()
    {
       parent::__construct();
    }
    public static function get($tabla)
    {
        $sql="SELECT * FROM ".$tabla."";
        if($sentencia=$this->_dbh->prepare($sql))
        {
            echo "Prepared.";
        }   
    } 
 }
getAll::get('users');
 ?>

Comments

0

DB class

<?php 
class DB
{
    var $_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());  
        }
    }
    function get_dbh(){
        return $this->_dbh;  
    }
    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()
    {
        $con = DB::Link();
        $this->_dbh= $con->get_dbh();
    }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.