498 lines
14 KiB
PHP
498 lines
14 KiB
PHP
<?php
|
|
// Class Mysql v1.7
|
|
// créé par Jean-Sébastien
|
|
// mis à jour décembre 2015
|
|
class clsMysql {
|
|
var $con;
|
|
var $last_id;
|
|
var $strDb;
|
|
|
|
function __construct($db = array()) {
|
|
$this->strDb = $db['db'];
|
|
$this->con = mysqli_connect($db['host'], $db['user'], $db['pass'], $db['db'], '3306') or die ('Error connecting to MySQL');
|
|
mysqli_set_charset($this->con, "utf8") or die ("Error loading character set utf8: %s"); // change character set to utf8
|
|
//mysqli_select_db($this->con, $db['db']) or die('Database ' . $db['db'] . ' does not exist!');
|
|
}
|
|
|
|
function __destruct() {
|
|
mysqli_close($this->con);
|
|
}
|
|
|
|
// fonction qui exécute la requête SQL
|
|
function fxQuery($sql) {
|
|
$qry = mysqli_query($this->con, $sql);
|
|
$this->last_id = mysqli_insert_id($this->con);
|
|
return $qry;
|
|
}
|
|
|
|
function fxFetchAssoc($qry) {
|
|
return mysqli_fetch_assoc($qry);
|
|
}
|
|
|
|
function fxFetchRow($qry) {
|
|
return mysqli_fetch_row($qry);
|
|
}
|
|
|
|
function fxGetNumRows($qry) {
|
|
return mysqli_num_rows($qry);
|
|
}
|
|
|
|
// fonction qui retourne la variable du résultat de la requête SQL
|
|
function fxGetVar($sql) {
|
|
return $this->fxFetchArray(0, $sql);
|
|
}
|
|
|
|
// fonction qui retourne un tableau contenant la ligne unique du résultat de la requête SQL
|
|
function fxGetRow($sql) {
|
|
return $this->fxFetchArray(1, $sql);
|
|
}
|
|
|
|
// fonction qui retourne un tableau contenant le résultat de la requête SQL
|
|
function fxGetResults($sql) {
|
|
return $this->fxFetchArray(2, $sql);
|
|
}
|
|
function fxGetResultstest($sql) {
|
|
echo($sql);exit;
|
|
return $this->fxFetchArray(9, $sql);
|
|
}
|
|
|
|
// fonction qui retourne un tableau contenant le résultat de la requête SQL avec le premier champ du SELECT comme index
|
|
function fxGetResultsKey($sql, $key = 0) {
|
|
return $this->fxFetchArray(3, $sql, $key);
|
|
}
|
|
|
|
// fonction qui met la requête dans un tableau
|
|
function fxFetchArray($p, $sql, $key = 0) {
|
|
$mem_results = null;
|
|
$nb = 0;
|
|
$qry = $this->fxQuery($sql);
|
|
|
|
if ($qry === false)
|
|
echo 'erreur ---------------------- <br>' . $sql . '<br>--------------------';
|
|
|
|
if ($qry != false)
|
|
$nb = $this->fxGetNumRows($qry);
|
|
|
|
if ($nb > 0) {
|
|
switch ($p) {
|
|
// fxGetVar
|
|
case 0:
|
|
$rec = mysqli_fetch_row($qry);
|
|
$mem_results = $rec[0];
|
|
break;
|
|
|
|
// fxGetRow
|
|
case 1:
|
|
$rec = mysqli_fetch_assoc($qry);
|
|
|
|
foreach (array_keys($rec) as $field)
|
|
$mem_results[$field] = $rec[$field];
|
|
break;
|
|
|
|
// fxGetResults
|
|
case 2:
|
|
$ctr = 1;
|
|
|
|
while ($rec = mysqli_fetch_assoc($qry)) {
|
|
foreach (array_keys($rec) as $field)
|
|
$mem_results[$ctr][$field] = $rec[$field];
|
|
|
|
$ctr++;
|
|
}
|
|
break;
|
|
|
|
// fxGetResultsKey
|
|
case 9:
|
|
$ctr = 1;
|
|
|
|
if ($qry && mysqli_num_rows($qry) > 0) { // Vérification que la requête renvoie des résultats
|
|
while ($rec = mysqli_fetch_assoc($qry)) {
|
|
if ($rec) { // Vérification que $rec n'est pas false
|
|
foreach (array_keys($rec) as $field) {
|
|
// Vérification que le champ existe dans $rec
|
|
$mem_results[$ctr][$field] = isset($rec[$field]) ? $rec[$field] : null;
|
|
}
|
|
$ctr++;
|
|
}
|
|
}
|
|
}
|
|
|
|
break;
|
|
case 3:
|
|
$ctr = 1;
|
|
|
|
while ($rec = mysqli_fetch_assoc($qry)) {
|
|
if ($key === 0) {
|
|
$mem_clef = array_keys($rec);
|
|
$key = $mem_clef[0];
|
|
}
|
|
|
|
$counter = 1;
|
|
|
|
foreach (array_keys($rec) as $field) {
|
|
if ($counter != 1)
|
|
$mem_results[$rec[$key]][$field] = $rec[$field];
|
|
|
|
$counter++;
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 4:
|
|
$ctr = 1;
|
|
$mem_lastkey = "";
|
|
|
|
while ($rec = mysqli_fetch_assoc($qry)) {
|
|
$mem_clef = array_keys($rec);
|
|
|
|
if ($mem_lastkey != $rec[$mem_clef[0]]) {
|
|
$mem_lastkey = $rec[$mem_clef[0]];
|
|
$ctr = 1;
|
|
}
|
|
else
|
|
$ctr++;
|
|
|
|
$counter = 1;
|
|
|
|
foreach (array_keys($rec) as $field) {
|
|
if ($counter != 1)
|
|
$mem_results[$mem_lastkey][$ctr][$field] = $rec[$field];
|
|
|
|
$counter++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $mem_results;
|
|
}
|
|
|
|
// fonction qui retourne les détails des champs selon le SQL
|
|
function fxGetFields($sql) {
|
|
$qry = $this->fxQuery($sql);
|
|
$nb = mysqli_num_fields($qry);
|
|
|
|
if($qry === false)
|
|
echo('erreur ---------------------- <br>'.$sql.'<br>--------------------');
|
|
|
|
if ($nb > 0) {
|
|
$intCtr = 0;
|
|
|
|
while ($intCtr < $nb) {
|
|
$rec = mysqli_fetch_field($qry);
|
|
|
|
foreach ($rec as $name => $value) {
|
|
if ($name == 'name' || $name == 'numeric')
|
|
$mem_results[$intCtr + 1][$name] = $value;
|
|
}
|
|
|
|
$intCtr++;
|
|
}
|
|
|
|
return $mem_results;
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
// fonction qui retourne les infos des champs du table donnée NOV 2014
|
|
function fxGetInfoFields($strTables = '', $strFields = '') {
|
|
$strWhere = '';
|
|
|
|
if ($strTables != '') {
|
|
$tabTables = explode(',', $strTables);
|
|
$strWhere = " TABLE_NAME IN('" . implode("','", $tabTables) . "')";
|
|
}
|
|
elseif ($strFields != '') {
|
|
$tabFields = explode(',', $strFields);
|
|
$strWhere = " COLUMN_NAME IN('" . implode("','", $tabFields) . "')";
|
|
}
|
|
|
|
$strSql = "SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_KEY, EXTRA, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE FROM information_schema.COLUMNS WHERE $strWhere AND TABLE_SCHEMA = '" . $this->strDb . "'";
|
|
$tabInfoFields = $this->fxGetResultsKey($strSql);
|
|
|
|
return $tabInfoFields;
|
|
}
|
|
|
|
//fonction pour échapper les caractères pour les variables post ou get
|
|
//param: valeur
|
|
//retourne la valeur échappée
|
|
function fxEscape($values) {
|
|
if (is_array($values))
|
|
$values = array_map(array($this, 'fxEscape'), $values);
|
|
else {
|
|
// Stripslashes
|
|
// MSIN-CON-259
|
|
//if (get_magic_quotes_gpc())
|
|
//$values = stripslashes($values);
|
|
|
|
if ((int)ini_get('magic_quotes_sybase'))
|
|
$values = str_replace("''", "'", $values);
|
|
|
|
$values = str_replace('"', '"', $values);
|
|
$values = mysqli_real_escape_string($this->con, $values);
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
|
|
function fxGetLastId() {
|
|
return $this->last_id;
|
|
}
|
|
}
|
|
|
|
//fonction pour rescapper les caractères pour les variables post ou get
|
|
//param: valeur ou tableau de valeurs
|
|
//retourne la valeur rescappée
|
|
function fxUnescape($values) {
|
|
if (is_array($values))
|
|
$values = array_map(array($this, 'fxUnescape'), $values);
|
|
else {
|
|
// Stripslashes
|
|
// MSIN-CON-259
|
|
//if (get_magic_quotes_gpc())
|
|
// $values = stripslashes($values);
|
|
|
|
$values = str_replace('"', '"', $values);
|
|
|
|
// if ((int)ini_get('magic_quotes_sybase'))
|
|
// $values = str_replace("''", "'", $values);
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
/*class clsMysql {
|
|
var $con;
|
|
var $last_id;
|
|
var $strDb;
|
|
|
|
function __construct($db = array()) {
|
|
$this->strDb = $db['db'];
|
|
$this->con = mysqli_connect($db['host'], $db['user'], $db['pass']) or die ('Error connecting to MySQL');
|
|
mysqli_select_db($this->con, $db['db']) or die('Database ' . $db['db'] . ' does not exist!');
|
|
}
|
|
|
|
function __destruct() {
|
|
mysqli_close($this->con);
|
|
}
|
|
|
|
// fonction qui exécute la requête SQL
|
|
function fxQuery($sql) {
|
|
$qry = mysqli_query($this->con, $sql);
|
|
$this->last_id = mysqli_insert_id($this->con);
|
|
return $qry;
|
|
}
|
|
|
|
function fxFetchAssoc($qry) {
|
|
return mysqli_fetch_assoc($qry);
|
|
}
|
|
|
|
function fxFetchRow($qry) {
|
|
return mysqli_fetch_row($qry);
|
|
}
|
|
|
|
function fxGetNumRows($qry) {
|
|
return mysqli_num_rows($qry);
|
|
}
|
|
|
|
// fonction qui retourne la variable du résultat de la requête SQL
|
|
function fxGetVar($sql) {
|
|
return $this->fxFetchArray(0, $sql);
|
|
}
|
|
|
|
// fonction qui retourne un tableau contenant la ligne unique du résultat de la requête SQL
|
|
function fxGetRow($sql) {
|
|
return $this->fxFetchArray(1, $sql);
|
|
}
|
|
|
|
// fonction qui retourne un tableau contenant le résultat de la requête SQL
|
|
function fxGetResults($sql) {
|
|
return $this->fxFetchArray(2, $sql);
|
|
}
|
|
|
|
// fonction qui retourne un tableau contenant le résultat de la requête SQL avec le premier champ du SELECT comme index
|
|
function fxGetResultsKey($sql) {
|
|
return $this->fxFetchArray(3, $sql);
|
|
}
|
|
|
|
// fonction qui met la requête dans un tableau
|
|
function fxFetchArray($p, $sql) {
|
|
$nb = 0;
|
|
$qry = $this->fxQuery($sql);
|
|
|
|
if ($qry === false)
|
|
echo 'erreur ---------------------- <br>' . $sql . '<br>--------------------';
|
|
|
|
if ($qry != false)
|
|
$nb = $this->fxGetNumRows($qry);
|
|
|
|
if ($nb > 0) {
|
|
switch ($p) {
|
|
// fxGetVar
|
|
case 0:
|
|
$rec = mysqli_fetch_row($qry);
|
|
$mem_results = $rec[0];
|
|
break;
|
|
|
|
// fxGetRow
|
|
case 1:
|
|
$rec = mysqli_fetch_assoc($qry);
|
|
|
|
foreach (array_keys($rec) as $field)
|
|
$mem_results[$field] = $rec[$field];
|
|
break;
|
|
|
|
// fxGetResults
|
|
case 2:
|
|
$ctr = 1;
|
|
|
|
while ($rec = mysqli_fetch_assoc($qry)) {
|
|
foreach (array_keys($rec) as $field)
|
|
$mem_results[$ctr][$field] = $rec[$field];
|
|
|
|
$ctr++;
|
|
}
|
|
break;
|
|
|
|
// fxGetResultsKey
|
|
case 3:
|
|
$ctr = 1;
|
|
|
|
while ($rec = mysqli_fetch_assoc($qry)) {
|
|
$mem_clef = array_keys($rec);
|
|
$key = $mem_clef[0];
|
|
|
|
$counter = 1;
|
|
|
|
foreach (array_keys($rec) as $field) {
|
|
if ($counter != 1)
|
|
$mem_results[$rec[$key]][$field] = $rec[$field];
|
|
|
|
$counter++;
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 4:
|
|
$ctr = 1;
|
|
$mem_lastkey = "";
|
|
|
|
while ($rec = mysqli_fetch_assoc($qry)) {
|
|
$mem_clef = array_keys($rec);
|
|
|
|
if ($mem_lastkey != $rec[$mem_clef[0]]) {
|
|
$mem_lastkey = $rec[$mem_clef[0]];
|
|
$ctr = 1;
|
|
}
|
|
else
|
|
$ctr++;
|
|
|
|
$counter = 1;
|
|
|
|
foreach (array_keys($rec) as $field) {
|
|
if ($counter != 1)
|
|
$mem_results[$mem_lastkey][$ctr][$field] = $rec[$field];
|
|
|
|
$counter++;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $mem_results;
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
// fonction qui retourne les détails des champs selon le SQL
|
|
function fxGetFields($sql) {
|
|
$qry = $this->fxQuery($sql);
|
|
$nb = mysqli_num_fields($qry);
|
|
|
|
if($qry === false)
|
|
echo('erreur ---------------------- <br>'.$sql.'<br>--------------------');
|
|
|
|
if ($nb > 0) {
|
|
$intCtr = 0;
|
|
|
|
while ($intCtr < $nb) {
|
|
$rec = mysqli_fetch_field($qry);
|
|
|
|
foreach ($rec as $name => $value) {
|
|
if ($name == 'name' || $name == 'numeric')
|
|
$mem_results[$intCtr + 1][$name] = $value;
|
|
}
|
|
|
|
$intCtr++;
|
|
}
|
|
|
|
return $mem_results;
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
// fonction qui retourne les infos des champs du table donnée NOV 2014
|
|
function fxGetInfoFields($strTables = '', $strFields = '') {
|
|
$strWhere = '';
|
|
|
|
if ($strTables != '') {
|
|
$tabTables = explode(',', $strTables);
|
|
$strWhere = " TABLE_NAME IN('" . implode("','", $tabTables) . "')";
|
|
}
|
|
elseif ($strFields != '') {
|
|
$tabFields = explode(',', $strFields);
|
|
$strWhere = " COLUMN_NAME IN('" . implode("','", $tabFields) . "')";
|
|
}
|
|
|
|
$strSql = "SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_KEY, EXTRA, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE FROM information_schema.COLUMNS WHERE $strWhere AND TABLE_SCHEMA = '" . $this->strDb . "'";
|
|
$tabInfoFields = $this->fxGetResultsKey($strSql);
|
|
|
|
return $tabInfoFields;
|
|
}
|
|
|
|
//fonction pour échapper les caractères pour les variables post ou get
|
|
//param: valeur
|
|
//retourne la valeur échappée
|
|
function fxEscape($values) {
|
|
if (is_array($values))
|
|
$values = array_map(array($this, 'fxEscape'), $values);
|
|
else {
|
|
// Stripslashes
|
|
if (get_magic_quotes_gpc())
|
|
$values = stripslashes($values);
|
|
|
|
if ((int)ini_get('magic_quotes_sybase'))
|
|
$values = str_replace("''", "'", $values);
|
|
|
|
$values = str_replace('"', '"', $values);
|
|
$values = mysqli_real_escape_string($this->con, $values);
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
|
|
function fxGetLastId() {
|
|
return $this->last_id;
|
|
}
|
|
}
|
|
|
|
//fonction pour rescapper les caractères pour les variables post ou get
|
|
//param: valeur ou tableau de valeurs
|
|
//retourne la valeur rescappée
|
|
function fxUnescape($values) {
|
|
if (is_array($values))
|
|
$values = array_map(array($this, 'fxUnescape'), $values);
|
|
else {
|
|
// Stripslashes
|
|
if (get_magic_quotes_gpc())
|
|
$values = stripslashes($values);
|
|
|
|
$values = str_replace('"', '"', $values);
|
|
|
|
if ((int)ini_get('magic_quotes_sybase'))
|
|
$values = str_replace("''", "'", $values);
|
|
}
|
|
|
|
return $values;
|
|
}*/ |