forked from kokonior/PHP-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
49 lines (38 loc) · 1.15 KB
/
Database.php
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
<?php
class Database
{
private $mysqli;
private $HOST = 'localhost';
private $USER = 'username';
private $PASS = 'password';
private $DBNAME = 'database_name';
private static $INSTANCE = null;
public function __construct()
{
$this->mysqli = new mysqli($this->HOST, $this->USER, $this->PASS, $this->DBNAME);
if(mysqli_connect_error() ) die ("Connection Fail");
}
// singleton pattern
public static function GetInstance()
{
if(!isset(self::$INSTANCE)) self::$INSTANCE = new Database();
return self::$INSTANCE;
}
public function Insert($table, $fields = array())
{
$collum = implode(", ", array_keys($fields));
$valuesArray = array();
$i = 0;
foreach($fields as $key => $values)
{
if(is_int($values)) $valuesArray[$i] = $values;
else $valuesArray[$i] = "'".$values."'";
$i++;
}
$values = implode(", ", $valuesArray);
$query = "INSERT INTO $table ($collum) VALUES ($values)";
if ($this->mysqli->query($query)) return true;
else return false;
}
}
?>