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
| <?php
class Database
{
private $host = 'localhost';
private $user = '';
private $pass = '';
private $dbname = '';
private $dbh;
private $stmt;
/*** _construct Start ***/
public function __construct()
{
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = [
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::MYSQL_ATTR_FOUND_ROWS => true
];
// Create a new PDO instanace
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
/*** _construct EnD ***/
/*** function to SELECT from the database Start ***/
public function select($table, $rows = '*', $join = null, $where = null, $group = null, $order = null, $limit = null)
{
// get fields
if (is_array($rows)) :
$fields = implode(', ', $rows);
else :
$fields = '*';
endif;
// Create query from the variables passed to the function
$sql = 'SELECT ' . $fields . ' FROM ' . $table;
if ($join != null) {
$sql .= ' JOIN ' . $join;
}
if ($where != null) {
$sql .= ' WHERE ' . $where;
}
if ($group != null) {
$sql .= ' GROUP BY ' . $group;
}
if ($order != null) {
$sql .= ' ORDER BY ' . $order;
}
if ($limit != null) {
$sql .= ' LIMIT ' . $limit;
}
//echo $sql . '<br>';
$this->sql = $sql;
return $this->sql;
}
/*** function to SELECT from the database EnD ***/
/*** function to INSERT to the database Start ***/
public function insert($table, $fields = [])
{
$sql = 'INSERT INTO ' . $table . ' (' . implode(', ', array_keys($fields)) . ') VALUES ("' . implode('", "', $fields) . '")';
echo 'Insert: ' . $sql . '<br>';
$this->sql = $sql;
//return $this->sql;
}
/*** function to INSERT to the database EnD ***/
/*** function to UPDATE database Start ***/
public function update($table, $fields = [], $where = null)
{
$args = [];
foreach ($fields as $fk => $fv) :
$fk = $this->strSafe($fk); // protect value against XSS attack
$args[] .= $fk . ' = :' . $fk;
endforeach;
$sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $args);
if ($where != null) {
$sql .= ' WHERE ' . $where;
}
$this->sql = $sql;
return $this->sql;
}
/*** function to UPDATE database EnD ***/
/*** function to delete from database Start ***/
public function delete($table, $where = null)
{
if ($where == null) :
return false;
else :
$sql = 'DELETE FROM ' . $table;
if ($where != null) {
$sql .= ' WHERE ' . $where;
}
endif;
$this->sql = $sql;
return $this->sql;
}
/*** function to delete from database Start ***/
public function showCols($table)
{
$sql = 'SHOW COLUMNS FROM ' . $table;
$this->sql = $sql;
return $this->sql;
}
/*** function to prepare query Start ***/
public function prepare()
{
return $this->stmt = $this->dbh->prepare($this->sql);
}
/*** function to prepare query EnD ***/
/*** function to bind query Start ***/
public function bind($param, $value, $type = null)
{
if (is_null($type)) :
switch (true):
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
endswitch; // end switch (true):
endif; // end if (is_null($type)):
$value = $this->strSafe($value); // protect value against XSS
$this->stmt->bindParam($param, $value, $type); // ligne 179
}
/*** function to bind query EnD ***/
/*** function to execute query Start ***/
public function execute()
{
return $this->stmt->execute();
}
/*** function to execute query EnD ***/
/*** function to execute and fetch Start ***/
public function resultset()
{
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
/*** function to execute and fetch EnD ***/
/*** function to count rows Start ***/
public function rowCount()
{
return $this->stmt->rowCount();
}
/*** function to count rows EnD ***/
/*** function to get last inserted ID Start ***/
public function lastInsertId()
{
return $this->dbh->lastInsertId();
}
/*** function to get last inserted ID EnD ***/
public function beginTransaction()
{
return $this->dbh->beginTransaction();
}
public function endTransaction()
{
return $this->dbh->commit();
}
public function cancelTransaction()
{
return $this->dbh->rollBack();
}
public function debugDumpParams()
{
return $this->stmt->debugDumpParams();
}
public function strSafe($string)
{
$string = trim(strip_tags(($string)));
$string = preg_replace('/\s+/', ' ', $string); // remove more than one space
$this->string = $string;
return $this->string;
}
} // end class |
Partager