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.
static
from the definition ofpublic 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