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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
| <?php
include 'config.php';
class DATABASE {
// Hote
private $host = 'localhost';
// User
private $user = '';
// Password
private $password = '';
// Databse
private $database = '';
// Persistency
private $persistency = false;
// Port
private $port = NULL;
// DB DSN
private $dsn = '';
// PDO
private $pdo = NULL;
// Error mode
private $errorMode = 'WARNING';
// Constante pour la methode quote
const PARAM_STR = 1; // Cast du type STR;
const PARAM_INT = 2; // Cast du type INT
const PARAM_FLOAT = 3; // Cast du FLOAT
const PARAM_BOOL = 4; // Cast du type BOOL
// Constante pour le type d'eereur
const ERRMODE_EXCEPTION = 'E_EXCEPTION';
const ERRMODE_WARNING = 'E_WARNING';
//Exception
private $exception = true;
// Instance
protected static $instance;
// Gestion de l'exception du à l'appel du constrcuteur
public static function exception_handler($exception) {
// Output the exception details
trigger_error($exception->getMessage(),$exception->getCode());
}
/***
* - @desc : Constructor
* - @params : Array $host,$user,$password,$database,$persistency,$port
* - @return : none
*/
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 : Crée une instance de la class et éffectue la connexion
* - @params : Array $host,$user,$password,$database,$persistency,$port
* - @return : self::instance
*/
public static function connect($params,$options=array()) {
if (!isset(self::$instance)) {
self::$instance = new self($params,$options);
}
return self::$instance;
}
/***
* - @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 : Gestion des transactions
* - @params : Bool $autocomit
* - @return : Bool
*/
public function AutoCommit($autocommit = true) {
return $this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, $autocommit);
}
/***
* - @desc : Démarre la transaction
* - @params : None
* - @return : Bool
*/
public function Begin() {
return $this->pdo->beginTransaction();
}
/***
* - @desc : Valide la requête
* - @params : None
* - @return : Bool
*/
public function Commit() {
return $this->pdo->commit();
}
/***
* - @desc : Annule la requête
* - @params : None
* - @return : Bool
*/
public function RollBack() {
return $this->pdo->rollBack();
}
/***
* - @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);
}
/***
* - @desc : Execute une requête préparée
* - @params : String $quary, Statement $stmt
* - @return : Query result
*/
public function execute($varArray,$stmt) {
// Le paramètre doit être un Array
if( is_array($varArray) ) {
// Si le tableau des parametre est vide on execute la requête preparé sans paramètre
if( empty($varArray) ) {
$stmt->execute();
// Sinon on execute la requête préparée avec les paramètres
} else {
$stmt->execute($varArray);
}
return $stmt;
} else {
// Mode exception
if( $this->exception ) {
throw new Exception('<pre><b>FATAL ERROR : '.__METHOD__.'</b> except first given parameter to be an array</pre>',0);
} else {
return false;
}
}
}
/***
* - @desc : Fecth Mode Array
* - @params : Resource query result
* - @return : query result rows
*/
public function fetchAll($results) {
return $results->fetchAll();
}
/***
* - @desc : Fecth Mode Object
* - @params : Resource query result
* - @return : query result rows
*/
public function fetchObject($results) {
return $results->fetch(PDO::FETCH_OBJ);
}
/***
* - @desc : Fecth Mode Assoc
* - @params : Resource query result
* - @return : query result rows
*/
public function fetchAssoc($results) {
return $results->fetch(PDO::FETCH_ASSOC);
}
/***
* - @desc : Fecth Mode Num ROw
* - @params : Resource query result
* - @return : query result rows
*/
public function fetchRow($results) {
return $results->fetch(PDO::FETCH_NUM);
}
/***
* - @desc : Fecth Mode Array
* - @params : Resource query result
* - @return : query result rows
*/
public function fetchArray($results) {
return $results->fetch(PDO::FETCH_BOTH);
}
/***
* - @desc : Retourne le nombre de resultat d'une requête
* - @params : None
* - @return : Int numrows
*/
public function rowCount() {
return $this->pdo->rowCount();
}
/***
* - @desc : Retourne l'id du dernier enregistrement
* - @params : Void
* - @return : Int id
*/
public function lastInsertId($options = array()) {
return $this->pdo->lastInsertId();
}
/***
* - @desc : Librer les resultats de la requête
* - @params : results
* - @return : Bool
*/
public function closeCursor($results = NULL) {
return $this->pdo->closeCursor();
}
/***
* quote
*/
public function quote($value,$type = DATABASE::PARAM_STR) {
switch($type) {
case self::PARAM_INT :
$value = $this->pdo->quote($value,PDO::PARAM_INT);
break;
case self::PARAM_FLOAT :
$value = $this->pdo->quote($value,PDO::PARAM_FLOAT);
break;
case self::PARAM_BOOL :
$value = $this->pdo->quote($value,PDO::PARAM_BOOL);
break;
default:
$value = $this->pdo->quote($value,PDO::PARAM_STR);
break;
}
return $value;
}
public function errorInfo($stmt='') {
if(empty($stmt) ) {
return $this->pdo->errorInfo();
} else {
return $stmt->errorInfo();
}
}
}
$db = DATABASE::connect($dbcfg,array('ATTR_ERRMODE'=>DATABASE::ERRMODE_WARNING));
$stmt = $db->prepare("SELECT * FROM titrs WHERE id=?");
if( $db->execute(array(1),$stmt) === FALSE ) {
// Je doit être ici mai ce if saute
print_r ($stmt->errorInfo());
} else {
print_r($db->fetchAll($stmt));
}
?> |
Partager