-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.php
128 lines (98 loc) · 3.25 KB
/
DB.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
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
require_once 'components/functions.php';
class DataBase
{
public $dblocation;
public $dbname;
public $dbuser;
public $dbpasswd;
public function __construct()
{
$this->dblocation = "localhost";
$this->dbname = "wikipdf";
$this->dbuser = "root";
$this->dbpasswd = "";
}
public function connect()
{
$db = new mysqli($this->dblocation, $this->dbuser, $this->dbpasswd, $this->dbname);
mysqli_set_charset($db, "utf8");
if($db->connect_errno) {
die("Ошибка доступа к MySQL");
}
echo "DB is connected to<br>";
return $db;
}
public function createIninitalArticleTable($db, string $DBtableName)
{
//CREATE TABLE wikipdf.$tableName
//(url VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL)
//ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_general_ci;
$sql = "CREATE TABLE wikipdf.$DBtableName(url VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_general_ci;";
$rs = mysqli_query($db, $sql) or trigger_error(mysqli_error($rs) . " in " . $sql);
echo "<br><b>Создана таблица: $DBtableName<b><br>";
}
public function dropTable($db, string $tN)
{
$sql = "DROP TABLE $tN";
$rs = mysqli_query($db, $sql) or trigger_error(mysqli_error($rs) . " in " . $sql);
echo "Уничтожена таблица: $tN<br>";
}
//ПРОВЕРКИ - СУЩЕСТВУЕТ ЛИ ТАБЛИЦА И НЕ ПУСТАЯ ЛИ ОНА
public function tableExists($db, string $tN)
{
$sql = "SHOW TABLES LIKE " . "'" . "$tN" . "'";
$rs = mysqli_query($db, $sql) or trigger_error(mysqli_error($rs) . " in " . $sql);
return (mysqli_num_rows($rs) > 0);
}
public function isTableEmpty($db, string $tN)
{
//$sql = "SELECT count(*) FROM $tN";
$sql = "SELECT * FROM $tN";
$rs = mysqli_query($db, $sql) or trigger_error(mysqli_error($rs) . " in " . $sql);
if (mysqli_num_rows($rs) == 0) //table row is empty
{
return true; //table row is empty
} else return false;
}
//OPERATIONS WITH ROWS
public function insertRow($db, $tN, string $r)
{
//INSERT INTO Маршрутизатор (url) VALUE ("ettet")
$sql = "INSERT INTO $tN (url) VALUE (" . '"' . trim($r) . '"' . ")";
$rs = mysqli_query($db, $sql) or trigger_error(mysqli_error($rs) . " in " . $sql);
}
public function selectFirstRow($db, $tN)
{
$sql = "SELECT * FROM $tN url LIMIT 1";
$rs = mysqli_query($db, $sql) or trigger_error(mysqli_error($rs) . " in " . $sql);
return $rs;
}
public function deleteRow($db, $tN, $r)
{
$sql = "DELETE FROM $tN WHERE url = " . '"' . $r . '"';
$rs = mysqli_query($db, $sql) or trigger_error(mysqli_error($rs) . " in " . $sql);
}
public function selectAll($db, $tN)
{
$sql = "SELECT * FROM $tN";
$rs = mysqli_query($db, $sql) or trigger_error(mysqli_error($rs) . " in " . $sql);
return $rs;
}
public function showAll($rs)
{
$allUrl = [];
while ($row = $rs->fetch_assoc())
{
$r = $row['url'];
$allUrl[] = $r;
//printMessage($r, "");
}
return $allUrl;
}
public function close()
{
$db->close();
echo 'Connection closed';
}
}