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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| protected function __construct($params,$options = array()) {
$this->host = array_key_exists('host', $params) ? $params['host'] : 'localhost';
$this->user = array_key_exists('user', $params) ? $params['user'] : '';
$this->password = array_key_exists('password',$params) ? $params['password'] : '';
$this->database = array_key_exists('database',$params) ? $params['database'] : '';
$this->persistency = array_key_exists('persistency',$params) ? $params['persistency'] : false;
$this->port = array_key_exists('port',$params) ? $params['port'] : '';
$this->dsn = 'mysql:host='.$this->host.';dbname='.$this->database.'';
// Temporarily change the PHP exception handler while we . . .
//set_exception_handler(array(__CLASS__, 'exception_handler'));
$this->pdo = new PDO ($this->dsn, $this->user, $this->password);
if( !empty($options) ) {
// On cherche le type de handler choisit par l'utilisateur
if( array_key_exists('ATTR_ERRMODE', $options) ) {
// Gestion des erreur avec les exceptions
if( strtoupper($options['ATTR_ERRMODE']) == self::ERRMODE_EXCEPTION ) {
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Gestion des erreur en mode standard
} elseif( strtoupper($options['ATTR_ERRMODE']) == self::ERRMODE_WARNING ) {
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
// Le mode de gestion des erreur est inconue
} else {
trigger_error(__CLASS__.' :: impossible d\'activer le mode de gestion d\'erreur définit',E_USER_ERROR);
}
$this->exception = in_array(self::ERRMODE_WARNING,$options) || in_array(self::ERRMODE_EXCEPTION,$options) ? true : false;
} else {
// On définit les Exceptions comme mode d'erreur par default
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
$this->pdo->exec("SET CHARACTER SET utf8");
// Change the exception handler back to whatever it was before
//restore_exception_handler();
}
/***
* - @desc : Execute une requête SQL
* - @params : String $sql
* - @return : Query result
*/
public function Query($sql) {
if( strpos(strtoupper($sql),"SELECT") == 0 ) {
return $this->pdo->Query($sql);
} else {
return $this->pdo->exec($sql);
}
/***
* - @desc : Prépare la requête
* - @params : String $quary,Array $option
* - @return : Query statement
*/
public function prepare($query,$options = array()) {
return $this->pdo->prepare($query);
}
}
.
.........
//ensuite
$db = DATABASE::connect($dbcfg,array('ATTR_ERRMODE'=>DATABASE::ERRMODE_EXCEPTION));
try {
$db->prepare("SELECT * FRM titres WHERE title=".$db->quote("One Time",DATABASE::PARAM_STR)."");
} catch(PDOException $e ) {
echo $e->getMessage();
}
// vous voyer il y'a bien un probelem dans la reque mais je n'ai aucune erreur
//en faisant sa aussi
DATABASE::connect($dbcfg,array('ATTR_ERRMODE'=>DATABASE::ERRMODE_WARNING));
if( $stmt = $db->prepare("SELECT * FRM titres WHERE title=".$db->quote("One Time",DATABASE::PARAM_STR)."") === FALSE ) {
echo "erreur"
} else {
echo "la requete s'est bien passé"
} |
Partager