voici la ligne lol :
$dbh = new PDO($host, $u, $p);
Pour ce qui est de l'exception c'est vrai que j'aurai du l'entourer d'un block try
comme ceci:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
function loaddata(){
global $host, $u, $p;
$dbh = new PDO($host, $u, $p);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try{
$stmt = $dbh->prepare("SELECT * FROM dbmachin WHERE machin = :machin");
}catch (Exception $e) {
$e->getMessage();
}
$stmt->bindParam(':machin',$this->machin, PDO::PARAM_STR);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$result = $stmt->fetchAll();
$stmt->closeCursor();
$stmt = null;
return $result;
} |
Pour ce qui est d'une class héritant de pdo j'ai penser à un truc :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class ExtendsException extends Exception
{
public function __construct($msg=null, $code=0)
{
parent::__construct($msg, $code);
}
public function MysqlConnexion()
{
if ( ! $dbh = new PDO($host, $u, $p) )
{
throw new ExtendsException('Impossible de se connecter à MySQL');
}
}
public function MysqlTable($sql)
{
$dbh = new PDO($host, $u, $p);
if (!$stmt = $dbh->prepare($sql))
{
throw new ExtendsException('Impossible de se connecter à MySQL');
}
}
}
function exception_handler($ExtendsException)
{
$ExtendsException->MysqlTable();
}
set_exception_handler('exception_handler'); |
Probléme je ne vois pas comment la mettre en place, je serai obliger de l'introduire dans chacune de mes class hors le coté pratique n'y est pas
Une idée ?
Partager