-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataBaseConnection.inc
128 lines (109 loc) · 3.53 KB
/
DataBaseConnection.inc
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
<?php
class DataBaseConnection
{
private $dbConnection = null;
private $host = '';
private $databaseName = '';
private $username = '';
function __construct($db)
{
$connection = [
'oldDb' => [
'host' => 'localhost',
'databaseName' => 'coupondunia',
'username' => 'root',
'password' => 'root',
],
'newDb' => [
'host' => 'localhost',
'databaseName' => 'cdapi',
'username' => 'root',
'password' => 'root',
],
];
$this->host = $connection[$db]['host'];
$this->databaseName = $connection[$db]['databaseName'];
$this->username = $connection[$db]['username'];
$this->dbConnection = new mysqli($connection[$db]['host'], $connection[$db]['username'], $connection[$db]['password'], $connection[$db]['databaseName']);
$this->dbConnection->set_charset('utf8');
if ($this->dbConnection->connect_error) {
die('Connect Error (' . $this->dbConnection->connect_errno . ') ' . $dbConnection->connect_error);
} else {
echo 'Success... ' . $this->dbConnection->host_info . PHP_EOL;
}
}
public function close()
{
$this->dbConnection->close();
}
public function hasColumn($table, $column)
{
$this->dbConnection->query("SELECT * FROM information_schema.COLUMNS WHERE
TABLE_SCHEMA = '" . $this->databaseName . "' AND TABLE_NAME = '" . $table . "'
AND COLUMN_NAME = '" . $column . "'");
return $this->dbConnection->affected_rows;
}
public function selectQuery($query)
{
$result = $this->dbConnection->query($query);
if ($result) {
return $result;
} else {
echo "Error Executing Query or no data found: " . $query . "<br/>";
}
}
public function setCharset($encode)
{
$this->dbConnection->set_charset($encode);
}
public function escapeString($value)
{
return $this->dbConnection->real_escape_string($value);
}
public function insertQuery($query)
{
$this->dbConnection->query($query);
if (1 > $this->dbConnection->affected_rows) {
echo 'Error Executing Query :' . $query . PHP_EOL;
echo $this->dbConnection->error . PHP_EOL;
}
}
public function updateQuery($query)
{
if (!$this->dbConnection->query($query)) {
echo 'Error Executing Query :' . $query . "<br/>";
echo $this->dbConnection->error . "<br/>";
}
}
public function truncateTable($table)
{
$this->dbConnection->query("truncate table $table");
if (!$this->dbConnection->affected_rows) {
return false;
}
return true;
}
public function executeQuery($query)
{
$this->dbConnection->query($query);
}
public function alterAutoIncrementValue($table, $column)
{
$value = $this->selectQuery("select max($column) as max_id from $table");
$query = "ALTER table $table AUTO_INCREMENT='" . $value->fetch_object()->max_id + 1 . "'";
$this->dbConnection->query($query);
}
public function disableForeignkeyCheck()
{
$this->dbConnection->query("SET FOREIGN_KEY_CHECKS = 0");
}
public function enableForeignkeyCheck()
{
$this->dbConnection->query("SET FOREIGN_KEY_CHECKS = 1");
}
public function getColumn($table)
{
return $this->dbConnection->query("desc $table");
}
}
?>