Created
August 12, 2015 23:33
-
-
Save Arc0re/c1a9b2b81cdc725af15d to your computer and use it in GitHub Desktop.
EPSI classe et méthodes pour MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SqlRequest | |
{ | |
private static $fh_db; | |
private function __construct() | |
{ | |
} | |
public static function createItem($table, $data) | |
{ | |
if (!is_array($data)) throw new InvalidArgumentException('Invalid argument : $data'); | |
$query = "INSERT INTO $table (`id`,"; | |
$newData = array(); | |
foreach ($data as $key => $value) { | |
$newData["`" . $key . "`"] = "'" . $value . "'"; | |
} | |
$query .= join(", ", array_keys($newData)); | |
$query .= ") VALUES (NULL, "; | |
$query .= join(", ", array_values($newData)); | |
$query .= ");"; | |
SqlRequest::executeQuery($query); | |
echo "Insertion d'une nouvelle entrée dans $table"; | |
if (mysql_error()) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
public static function executeQuery($query) | |
{ | |
echo "Requete : '$query''"; | |
$rs = mysql_query($query, SqlRequest::$fh_db); | |
if (mysql_error()) { | |
echo "<strong>REQUETE : </strong>\"$query\""; | |
throw new Exception(mysql_error()); | |
} | |
return $rs; | |
} | |
public static function updateItem($table, $id, $data) | |
{ | |
if (!is_array($data)) throw new InvalidArgumentException('Invalid argument : $data'); | |
$query = "UPDATE `$table` SET "; | |
$attr = array(); | |
foreach ($data as $key => $value) { | |
$attr[] = "`$key` = '$value'"; | |
} | |
$query .= join(", ", $attr); | |
$query .= " WHERE `id`='$id';"; | |
SqlRequest::executeQuery($query); | |
echo "Mise à jour de l'index $id de $table"; | |
if (mysql_error()) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
public static function getItem($table, $id) | |
{ | |
$query = "SELECT * FROM ".$table." WHERE `id`=".$id.";"; | |
$rs = SqlRequest::executeQuery($query); | |
$output = mysql_fetch_assoc($rs); | |
/*if(isset($_GET["DEBUG"]) && $_GET["DEBUG"] == 1){ | |
var_dump($query); | |
var_dump(mysql_fetch_assoc($rs)); | |
} | |
action_finished_feedback("Lecture de l'index $id de $table");*/ | |
if(!is_array($output)) return array(); | |
return array_map('utf8_encode', $output); | |
} | |
public static function getItems($table, $where = null) | |
{ | |
$query = "SELECT * FROM $table"; | |
if ($where != null) $query .= " WHERE $where"; | |
$query .= ";"; | |
$rs = SqlRequest::executeQuery($query); | |
$output = array(); | |
while ($row = mysql_fetch_assoc($rs)) { | |
$output[] = array_map('utf8_encode', $row); | |
} | |
echo "Lecture des lignes de $table"; | |
return $output; | |
} | |
public static function deleteItem($table, $id) | |
{ | |
$query = "DELETE FROM $table WHERE `id`='$id';"; | |
SqlRequest::executeQuery($query); | |
echo "Suppression définitive de la ligne $id de $table"; | |
if (mysql_error()) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
public static function getMaxId($table) | |
{ | |
$query = "SELECT MAX(id) as `maxid` from $table;"; | |
echo $query; | |
$rs = SqlRequest::executeQuery($query); | |
echo "Lecture de l'identifiant max de $table"; | |
$result = mysql_fetch_assoc($rs); | |
return intval($result["maxid"]); | |
} | |
public static function countItems($table, $where = null) | |
{ | |
$query = "select count(*) from $table"; | |
if ($where != null) $query .= " where $where"; | |
$query .= ";"; | |
$rs = SqlRequest::executeQuery($query); | |
echo "Comptage des lignes de $table"; | |
return (int)(mysql_fetch_assoc($rs)["count(*)"]); | |
} | |
public static function ouvertureBDD() | |
{ | |
$DBhost = ""; | |
$DBowner = ""; | |
$DBpw = ""; | |
$DBName = $DBowner; | |
SqlRequest::$fh_db = mysql_connect($DBhost, $DBowner, $DBpw); | |
if (mysql_error()) throw new Exception(mysql_error()); | |
echo "Connexion à la BDD"; | |
mysql_select_db($DBName); | |
if (mysql_error()) throw new Exception(mysql_error()); | |
echo "Connexion à la base FemEco"; | |
} | |
public static function fermetureBDD() | |
{ | |
mysql_close(SqlRequest::$fh_db); | |
if (mysql_error()) throw new Exception(mysql_error()); | |
echo "Fermeture de la connexion à la BDD"; | |
} | |
} | |
SqlRequest::ouvertureBDD(); | |
$exemple = SqlRequest::getItem('table', $idExemple); | |
SqlRequest::fermetureBDD(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment