-
Notifications
You must be signed in to change notification settings - Fork 0
/
DuSql.php
133 lines (100 loc) · 3.84 KB
/
DuSql.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
129
130
131
132
133
<?php
/*
Copyright 2021. Eduardo Programador
www.eduardoprogramador.com
DuSql -> Library for use of MySQL Database in Web Servers
Written by Eduardo Programador. All rights reserved.
Instructions:
1. Upload DuSql.php to your WebServer;
2. Include it on your Php File: require 'DuSql.php';
3. Initialize the Class wih the SQL constuctor: $dusql = new DuSql('user','password');
4. Set the server address info (ip): $dusql->infoServer('localhost');
5. Say the database name: $dusql->infoDatabase('dbname');
6. Set the debug option: 'yes' to show database logs and 'no' for no response: $duSql->debugMode('yes');
7. Connect to database: $dusql->start();
8. Insert some values or update: $duSql->operation('insert into mytable (id,test) values ('a','b')');
9. Retrieve some values: $duSql->select(status: 'select * from mytable', array of string colums here: array('id','test'));
*/
class DuSql {
private $sql_user;
private $sql_pass;
private $sql_server;
private $db;
private $con;
private $mode;
function __construct($user, $pass) {
$this->sql_user = $user;
$this->sql_pass = $pass;
}
function debugMode($answer) {
if(strcmp($answer,'yes') == 0) {
$this->mode = TRUE;
} else {
$this->mode = FALSE;
}
}
public function getValidString($str) : string {
$res = strip_tags($str);
$res = htmlentities($res);
return mysqli_real_escape_string($this->con,$res);
}
public function infoServer($server) : void {
$this->sql_server = $server;
}
public function infoDatabase($db) : void {
$this->db = $db;
}
public function start() : bool {
$this->con = new mysqli($this->sql_server,$this->sql_user,$this->sql_pass,$this->db);
if($this->con->connect_error) {
if($this->mode == TRUE) {
die("Connection Failed: " . $this->con->connect_error);
}
return FALSE;
} else {
if($this->mode == TRUE) {
echo("Connection Ok!");
}
return TRUE;
}
}
public function stop() : void {
$this->con->close();
}
public function operation($command) : bool {
$query = $command;
if($this->con->query($query) === TRUE) {
if($this->mode == TRUE) {
echo("Inserted");
}
return TRUE;
} else {
if($this->mode == TRUE) {
echo("Error: " . $query . $this->con->error);
}
return FALSE;
}
}
public function select($command, $arrayOfColumns) : array {
$temp = array();
$tempStr = array();
$i = 0;
$len = count($arrayOfColumns);
$len = count($arrayOfColumns);
$res = $this->con->query($command);
if($res->num_rows > 0) {
while($row = $res->fetch_assoc()) {
foreach($arrayOfColumns as $key) {
$tempStr[] = $row[$key];
}
$temp[] = $tempStr;
$tempStr = array();
}
return $temp;
} else {
return NULL;
}
}
}
?>