-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
68 lines (53 loc) · 1.53 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
class Database{
public $conn;
public function __construct(){
return $this->connect();
}
public function connect(){
$this->conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if(!$this->conn){
die("Connection failed: ". mysqli_connect_error());
return null;
}
return $this->conn;
}
public function disconnect(){
if(mysqli_close($this->conn))
return true;
else
return false;
}
public function checkIfTableExists($tblName){
$res = mysqli_query($this->conn, "SHOW TABLES LIKE '$tblName'");
return mysqli_num_rows($res);
}
public static function createTable($tblName, $query){
$db = new Database();
if($db->checkIfTableExists($tblName, $db->conn)){
Database::dropTable($tblName);
}
if(mysqli_query($db->conn, $query))
echo "$tblName table created successfully<br>";
else
echo "Failed to create $tblName table<br>";
}
public static function dropTable($tblName){
$db = new Database();
mysqli_query($db->conn, 'SET foreign_key_checks = 0');
if ($result = mysqli_query($db->conn, "SHOW TABLES")){
while($row = $result->fetch_array(MYSQLI_NUM)){
if($tblName == $row[0]){
if(mysqli_query($db->conn, 'DROP TABLE IF EXISTS '.$row[0])){
mysqli_query($db->conn, 'SET foreign_key_checks = 1');
echo $tblName." deleted successfully<br>";
return true;
}
}
}
}
echo $tblName." not deleted successfully<br>";
mysqli_query($migration->conn, 'SET foreign_key_checks = 1');
return false;
}
}