Comment utiliser la classe PDO sans extension
$GLOBALS['pdo'] = null; $GLOBALS['statement'] = null;
/**
* @param false $bPersistant
*/
function openDatabase($bPersistant = false) {
global $bddUser, $bddPassword, $bddHost, $bddPortFront;
$dsn = 'mysql:host='.$bddHost.';port='.$bddPortFront.';dbname=croisiere2018;charset=utf8mb4';
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_SILENT,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_PERSISTENT => $bPersistant,
];
try {
$GLOBALS['pdo'] = new \PDO($dsn, $bddUser, $bddPassword, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
}
/**
* @param $sql
* @param false $param
* @return array|false
*/
function dataBaseGetAssoc($sql, $param = false) {
$tabReturn = [];
$bPersistant = true;
if ($GLOBALS['pdo'] == null) {
openDatabase();
$bPersistant = false;
}
try {
$GLOBALS['statement'] = $GLOBALS['pdo']->prepare($sql);
if (is_array($param)) {
foreach ($param as $key => $value) {
$GLOBALS['statement']->bindParam($key, $value);
}
}
$result = $GLOBALS['statement']->execute();
if ($GLOBALS['statement']->errorCode() != '00000') {
writeLog("Error dataBaseGetAssoc() sql=".$sql."\nError".$GLOBALS['pdo']->errorInfo()."\nPARAMS=".serialize($param)."\n");
}
if($result == 1){
$tabReturn = $GLOBALS['statement']->fetchAll(\PDO::FETCH_ASSOC);
}
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if ($bPersistant == false) {
dataBaseClose();
}
return $tabReturn;
}
/**
* @param $sql
* @param false $param
* @return array|false
*/
function dataBaseGetCol($sql, $param = false) {
$tabReturn = [];
$bPersistant = true;
if ($GLOBALS['pdo'] == null) {
openDatabase();
$bPersistant = false;
}
try {
$GLOBALS['statement'] = $GLOBALS['pdo']->prepare($sql);
if (is_array($param)) {
foreach ($param as $key => $value) {
$GLOBALS['statement']->bindParam($key, $value);
}
}
$result = $GLOBALS['statement']->execute();
if ($GLOBALS['statement']->errorCode() != '00000') {
writeLog("Error dataBaseGetCol() sql=".$sql."\nError".$GLOBALS['pdo']->errorInfo()."\nPARAMS=".serialize($param)."\n");
}
if($result == 1){
$tabReturn = $GLOBALS['statement']->fetchAll(\PDO::FETCH_COLUMN, 0);
}
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if ($bPersistant == false) {
dataBaseClose();
}
return $tabReturn;
}
/**
* @param $sql
* @param false $param
* @return array|mixed
*/
function dataBaseGetOne($sql, $param = false) {
$tabReturn = [];
$bPersistant = true;
if ($GLOBALS['pdo'] == null) {
openDatabase();
$bPersistant = false;
}
try {
$GLOBALS['statement'] = $GLOBALS['pdo']->prepare($sql);
if (is_array($param)) {
foreach ($param as $key => $value) {
$GLOBALS['statement']->bindParam($key, $value);
}
}
$result = $GLOBALS['statement']->execute();
if ($GLOBALS['statement']->errorCode() != '00000') {
writeLog("Error dataBaseGetOne() sql=".$sql."\nError".$GLOBALS['pdo']->errorInfo()."\nPARAMS=".serialize($param)."\n");
}
if($result == 1){
$tabReturn = $GLOBALS['statement']->fetchColumn(0);
}
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if ($bPersistant == false) {
dataBaseClose();
}
return $tabReturn;
}
/**
* @param $sql
* @param false $param
* @return array|false
*/
function dataBaseExecute($sql, $param = false) {
$tabReturn = [];
$bPersistant = true;
if ($GLOBALS['pdo'] == null) {
openDatabase();
$bPersistant = false;
}
try {
$GLOBALS['statement'] = $GLOBALS['pdo']->prepare($sql);
if (is_array($param)) {
foreach ($param as $key => $value) {
$GLOBALS['statement']->bindParam($key, $value);
}
}
$result = $GLOBALS['statement']->execute($param);
if ($GLOBALS['statement']->errorCode() != '00000') {
writeLog("Error dataBaseExecute() sql=".$sql."\nError".$GLOBALS['pdo']->errorInfo()."\nPARAMS=".serialize($param)."\n");
}
if($result == 1){
$tabReturn = $GLOBALS['statement']->fetchAll(\PDO::FETCH_ASSOC);
}
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if ($bPersistant == false) {
dataBaseClose();
}
if (empty($tabReturn)) {
return false;
}
return $tabReturn;
}
function dataBaseClose() {
$GLOBALS['pdo'] = null;
$GLOBALS['statement'] = null;
}
Utilisation de la BDD avec une connexion par requête :
$tabResultats = dataBaseGetAssoc($sql);
Utilisation de la BDD avec une connexion unique pour plusieurs requêtes :
openDatabase(true);
while (1) {
if (dataBaseExecute($sql, $param) != false) {
writeLog("Requête SQL Error :\nRequête = ".$sql . "\nParametres = " . print_r($param, true) . "\n");
}}
dataBaseClose();
