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
| <?php
class SPDO
{
private $PDOInstance = null;
private static $instance = null;
const DEFAULT_SQL_USER = 'bdd';
const DEFAULT_SQL_HOST = 'localhost';
const DEFAULT_SQL_PASS = 'bdd';
const DEFAULT_SQL_DTB = 'bdd';
private function __construct()
{
$this->PDOInstance = new PDO('mysql:dbname='.self::DEFAULT_SQL_DTB.';host='.self::DEFAULT_SQL_HOST,self::DEFAULT_SQL_USER ,self::DEFAULT_SQL_PASS);
}
public static function getInstance()
{
if(is_null(self::$instance))
{
self::$instance = new SPDO();
}
return self::$instance;
}
public function query($query)
{
return $this->PDOInstance->query($query);
}
public function dbSelect($table, $fieldname=null, $id=null)
{
$sql = "SELECT * FROM `$table` WHERE `$fieldname`=:id";
$stmt = SPDO::getInstance()->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function rawSelect($sql)
{
return SPDO::getInstance()->query($sql);
}
public function rawQuery($sql)
{
SPDO::getInstance()->query($sql);
}
public function dbInsert($table, $values)
{
/*** snarg the field names from the first array member ***/
$fieldnames = array_keys($values[0]);
/*** now build the query ***/
$size = sizeof($fieldnames);
$i = 1;
$sql = "INSERT INTO $table";
/*** set the field names ***/
$fields = '( ' . implode(' ,', $fieldnames) . ' )';
/*** set the placeholders ***/
$bound = '(:' . implode(', :', $fieldnames) . ' )';
/*** put the query together ***/
$sql .= $fields.' VALUES '.$bound;
/*** prepare and execute ***/
$stmt = SPDO::getInstance()->prepare($sql);
foreach($values as $vals)
{
$stmt->execute($vals);
}
}
public function dbUpdate($table, $fieldname, $value, $pk, $id)
{
$sql = "UPDATE `$table` SET `$fieldname`='{$value}' WHERE `$pk` = :id";
$stmt = SPDO::getInstance()->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_STR);
$stmt->execute();
}
public function dbDelete($table, $fieldname, $id)
{
$sql = "DELETE FROM `$table` WHERE `$fieldname` = :id";
$stmt = SPDO::getInstance()->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_STR);
$stmt->execute();
}
}
$res = $SPDO->dbSelect('mpages', 'contenu', 3 );
foreach($res as $row)
{
echo $row['contenu'];
}
?> |
Partager