· 5 years ago · Apr 03, 2020, 04:36 PM
1<?php
2
3function SQLISortInsert($INPUT)
4{
5 $KEYS = array_keys($INPUT);
6 $VALS = array_values($INPUT);
7 $COLUMNS;
8 $VALUES;
9
10 for($X=0;$X<count($INPUT);++$X)
11 {
12 if($KEYS[$X] != "table")
13 {
14 $COLUMNS .= "`".$KEYS[$X]."`";
15 $VALUES .= "'".$VALS[$X]."'";
16 if($X < count($INPUT)-1)
17 {
18 $COLUMNS .= ", ";
19 $VALUES .= ", ";
20 }
21 }
22 }
23
24 return ["columns" => $COLUMNS, "values" => $VALUES];
25}
26
27function SQLISortUpdateRow($INPUT)
28{
29 $KEYS = array_keys($INPUT);
30 $VALS = array_values($INPUT);
31 $OUTPUT;
32 for($X=0;$X<count($INPUT);++$X)
33 {
34 $OUTPUT .= "`".$KEYS[$X]."` = '".$VALS[$X]."'";
35 if($X<count($INPUT)-1) $OUTPUT .= ", ";
36 }
37
38 return $OUTPUT;
39}
40
41class SQLInterface
42{
43 var $STATE;
44 var $ERROR;
45 var $SQLCONNECTION;
46
47 function __construct($DBINPUTS)
48 {
49 $this->STATE = 0;
50 $this->ERROR = "No Error";
51 $this->SQLCONNECTION = new mysqli($DBINPUTS['sqlhost'],$DBINPUTS['sqluser'],$DBINPUTS['sqlpass'],$DBINPUTS['sqldb']);
52 if (mysqli_connect_errno($SQLCONNECTION) > 0) $this->ERROR = mysqli_connect_error($SQLCONNECTION);
53 else $this->STATE = 1;
54 }
55
56 function CheckConnection()
57 {
58 return $this->STATE;
59 }
60
61 function GetError()
62 {
63 return $this->ERROR;
64 }
65
66 function RowExists($INPUTS)
67 {
68 $TABLE = $INPUTS['table'];
69 $KEY = $INPUTS['data']['key'];
70 $VALUE = $INPUTS['data']['value'];
71
72 $RESULT = $this->SQLCONNECTION->query("SELECT * FROM `$TABLE` WHERE `$KEY` = '$VALUE'");
73 if(mysqli_num_rows($RESULT) == 1) return "1";
74 if(mysqli_num_rows($RESULT) == 0) return "0";
75 else return mysqli_error($this->SQLCONNECTION);
76 }
77
78 function TableExists($INPUTS)
79 {
80 $TABLE = $INPUTS['table'];
81
82 $RESULT = $this->SQLCONNECTION->query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$TABLE'");
83 if(mysqli_num_rows($RESULT) == 1) return "1";
84 if(mysqli_num_rows($RESULT) == 0) return "0";
85 else return mysqli_error($this->SQLCONNECTION);
86 }
87
88 function ReadRow($INPUTS)
89 {
90 $TABLE = $INPUTS['table'];
91 $KEY = $INPUTS['data']['key'];
92 $VALUE = $INPUTS['data']['value'];
93
94 $RESULT = $this->SQLCONNECTION->query("SELECT * FROM `$TABLE` ORDER BY RAND() LIMIT 1");
95 if(mysqli_num_rows($RESULT) == 1) return mysqli_fetch_array($RESULT,MYSQLI_ASSOC);
96 if(mysqli_num_rows($RESULT) == 0) return "0";
97 else return mysqli_error($this->SQLCONNECTION);
98 }
99
100 function ReadRandomRow($INPUTS)
101 {
102 $TABLE = $INPUTS['table'];
103 $RESULT = $this->SQLCONNECTION->query("SELECT * FROM `$TABLE` WHERE `$KEY` = '$VALUE'");
104 if(mysqli_num_rows($RESULT) == 1) return mysqli_fetch_array($RESULT,MYSQLI_ASSOC);
105 if(mysqli_num_rows($RESULT) == 0) return "0";
106 else return mysqli_error($this->SQLCONNECTION);
107 }
108
109 function ReadColumn($INPUTS)
110 {
111 $TABLE = $INPUTS['table'];
112 $KEY = $INPUTS['data']['key'];
113
114 $RESULT = $this->SQLCONNECTION->query("SELECT $KEY FROM `$TABLE`");
115 if(mysqli_num_rows($RESULT) == 1) return mysqli_fetch_all($RESULT,MYSQLI_ASSOC);
116 if(mysqli_num_rows($RESULT) == 0) return "0";
117 else return mysqli_error($this->SQLCONNECTION);
118 }
119
120 function InsertRow($INPUTS)
121 {
122 $TABLE = $INPUTS['table'];
123 $SORT = SQLISortInsert($INPUTS['data']);
124 $COLUMNS = $SORT['columns'];
125 $VALUES = $SORT['values'];
126
127 if($this->SQLCONNECTION->query("INSERT INTO `$TABLE` ($COLUMNS) VALUES ($VALUES)") === TRUE) return "1";
128 else return mysqli_error($this->SQLCONNECTION);
129 }
130
131 function UpdateRow($INPUTS)
132 {
133 $TABLE = $INPUTS['table'];
134 $KEY = $INPUTS['key'];
135 $VALUE = $INPUTS['value'];
136 $BLOCK = SQLISortUpdateRow($INPUTS['data']);
137
138 if($this->SQLCONNECTION->query("UPDATE `$TABLE` SET $BLOCK WHERE `$KEY` = '$VALUE'") === TRUE) return "1";
139 else return mysqli_error($this->SQLCONNECTION);
140 }
141
142 function InsertTable($INPUTS)
143 {
144 $TABLE = $INPUTS['table'];
145 $BLOCK = $INPUTS['data'];
146
147 echo "CREATE TABLE IF NOT EXISTS `$TABLE` ($BLOCK);";
148
149 if($this->SQLCONNECTION->query("CREATE TABLE IF NOT EXISTS `$TABLE` ($BLOCK);") === TRUE) return "1";
150 else return mysqli_error($this->SQLCONNECTION);
151 }
152}
153
154?>