Sto tentando di connettermi, ma funziona il tutto se non inserisco un __construct().
Errore che ricevo
codice:
Fatal error: Uncaught Error: Call to a member function setAttribute() on null
Error: Call to a member function setAttribute() on null
Codice PHP:
<?php
//CLASSE PER LA CONNESIONE AL DB
class db {
public $connessione = null;
public function __construct()
{
$host = 'localhost';
$db = 'database';
$user = 'root';
$password = '';
try
{
$this->connessione = new PDO("mysql:host=".$host.";dbname=" .$db, $user, $password);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function connect()
{
return $this->connessione;
}
}
?>
Codice PHP:
<?php
// UTILIZZO CONNESSIONE
require_once'db.php';
class myclass extends db
{
public function __construct(){
....
}
function mytable($valore)
{
try {
$db=db::connect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // imposto l'attributo per il report degli errori
.....
$sql->execute();
}
$db = null; // chiusura della connessione
}
catch(PDOException $e)
{ echo $e->getMessage(); } // notifica in caso di errore nel tentativo di connessione
return $result;
}