0
I’m trying to use a class function as follows:
$database = database::select("SELECT * FROM eventos");
When I run the code, it returns the error:
Fatal error: Using $this when not in Object context in /var/www/html/folder/class/config.php online 27
The class is as follows:
<?php
class database{
protected static $driver = 'mysql';
protected static $host = 'localhost';
protected static $port = 3306;
protected static $user = 'root';
protected static $pass = 'vertrigo';
protected static $database = 'noweb';
public $link = null;
public function __construct(){
try{
$this->link = new PDO(self::$driver.":host=".self::$host.";port=".self::$port.";dbname=".self::$database, self::$user, self::$pass);
}catch(PDOException $i){
die("Ocorreu um erro ao fazer conexão: <code>".$i->getMessage()."</code>");
}
}
static function select($sql){
$query = $this->link->query($sql);
$rs = $query->fetchAll(PDO::FETCH_OBJ) or die(print_r($query->errorInfo(), true));
return $rs;
}
}
?>
How can I arrange this, since I want to call the duties of this class as follows:
database::FUNÇÃO('parametros');
Transform
$link
in static property and exchange the occurrences of$this
forself
– rray
@rray did it
$query =self::$link->query($sql);
and he returns me Fatal error: Call to a Member Function query() on a non-object– Alisson Acioli
Solved. I was able to solve by throwing the connection to a static function called connexion() and taking the connection from __Construct(); thus, in the function select(){ } I called the function self:connexion();
– Alisson Acioli