Advertisement
Guest User

Gb PHP

a guest
Mar 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. <?php
  2. class L{
  3. // Variable for connect to db
  4. static private $connection;
  5. static private $arr;
  6. // Variable for process freezing
  7. static private $freeze = false;
  8. // Private func for showStruct()
  9. static private function describe($table){
  10. $q = mysqli_query(self::$connection,"DESCRIBE `$table`");
  11. $qu = mysqli_query(self::$connection,"SELECT COUNT(1) FROM `$table`");
  12. $count = mysqli_fetch_array($qu);
  13. self::$arr = [];
  14. for($i = 0;$i < 250;$i++){
  15. self::$arr[$i] = mysqli_fetch_array($q);
  16. }
  17. foreach (self::$arr as $key => $value) {
  18. if(trim(gettype($value)) != 'array'){
  19. unset(self::$arr[$key]);
  20. }
  21. }
  22. }
  23. // Show tables in db
  24. public function tables(){
  25. $q = mysqli_query(self::$connection, "SHOW TABLES");
  26. $quer = mysqli_fetch_array($q);
  27. $arr = [];
  28. $i = 0;
  29. for($i = 0;$i<250;$i++){
  30. $i++;
  31. $arr[$i] = mysqli_fetch_array($q);
  32. }
  33. foreach ($arr as $key => $value) {
  34. if(trim(gettype($value)) != 'array'){
  35. unset($arr[$key]);
  36. array_values($arr);
  37. }
  38. }
  39. return $arr;
  40. }
  41. // Return struct of table
  42. public function showStruct($table){
  43. self::describe($table);
  44. return self::$arr;
  45. }
  46. // For cleaned sql
  47. public function sql($command){
  48. mysqli_query(self::$connection, $command);
  49. }
  50. // Freeze for "ALTER TABLE"
  51. public function freeze(){
  52. self::$freeze = true;
  53. }
  54. // If freeze = true
  55. public function unfreeze(){
  56. self::$freeze = false;
  57. }
  58. // Func for connect to db
  59. public function setup($host,$dbname,$login,$pass){
  60. self::$connection = mysqli_connect($host,$login,$pass,$dbname);
  61. }
  62. // Find in table at condition
  63. public function read($table,$condition){
  64.  
  65. $result = mysqli_query(self::$connection,"SELECT * FROM `$table` WHERE $condition ");
  66. return mysqli_fetch_array($result);
  67. }
  68. // Del the str at condition
  69. public function del($table,$condition){
  70. $del = "DELETE FROM `$table` WHERE $condition";
  71. $result = mysqli_query(self::$connection,$del);
  72. }
  73. public function eqval($str,$str2){
  74. return trim($str) == $str2;
  75. }
  76. //
  77. public function clear($table){
  78. $clr = "TRUNCATE TABLE `$table`";
  79. $result = mysqli_query(self::$connection,$clr);
  80. }
  81. // Выводит все данные из таблицы в виде двумерного массива
  82. public function readAll($table,$limit = false){
  83. $arr = array();
  84. $q = mysqli_query(self::$connection,"SELECT COUNT(1) FROM `$table`");
  85. $count = mysqli_fetch_array($q);
  86. if(!$limit){
  87. $limit = $count[0];
  88. }else{
  89. $limit = (int)$limit;
  90. }
  91. for($i = 1;$i < $limit;$i++){
  92. $result = mysqli_query(self::$connection,"SELECT * FROM `$table` WHERE id = $i");
  93. $arr[] = mysqli_fetch_array($result);
  94.  
  95. }
  96. foreach ($arr as $key => $value) {
  97. if(trim(gettype($value)) != 'array'){
  98. unset($arr[$key]);
  99. array_values($arr);
  100. }
  101. }
  102. array_values($arr);
  103. return $arr;
  104. }
  105. // Создаёт массив с которым будет работать update
  106. public function load($table,$id){
  107. $variable = ['table' => $table,'id' => (int)$id];
  108. return $variable;
  109. }
  110. // Обновляет записи в бд по id
  111. public function update($variable){
  112. $update = "
  113. UPDATE
  114. `".$variable['table']."`
  115. SET
  116. ";
  117. foreach ($variable as $key => $value) {
  118. if(trim($key) != 'table' && trim($key) != 'id'){
  119. if(trim(gettype($value)) == 'string'){
  120. $update = $update.'`'.$key.'` = \''.$value.'\',';
  121. }else{
  122. $update = $update.'`'.$key.'` = '.$value.',';
  123. }
  124. }
  125. }
  126. $update = substr($update,0,-1);
  127. $update = $update."
  128. WHERE
  129. id = ".$variable['id']."";
  130. $q = mysqli_query(self::$connection,$update);
  131. }
  132. // Создаёт массив для store
  133. public function dispense($table){
  134. $arr = ['table' => $table];
  135. return $arr;
  136. }
  137. // Создаёт таблицу если её нет, Закидывает записи в таблицу
  138. public function store($variable){
  139. if(self::$freeze == false){
  140. self::describe($variable['table']);
  141. $arr = [];
  142. $arr2 = [];
  143. $arr3 = [];
  144. foreach($variable as $key => $value){
  145. if(trim($key) != 'table'){
  146. $arr[$key] = $value;
  147. }
  148. }
  149. foreach (self::$arr as $key => $value) {
  150. if(trim(gettype($value)) == 'array'){
  151. foreach (self::$arr[$key] as $key => $value) {
  152. if($key == 'Field'){
  153. array_push($arr2,$value);
  154. }
  155. }
  156. }
  157. }
  158. foreach ($arr as $key => $value) {
  159. if(in_array($key, $arr2)){
  160.  
  161. }else{
  162. $arr3[$key] = $value;
  163. };
  164. }
  165. foreach ($arr3 as $key => $value) {
  166. if(trim($key) != 'table'){
  167. if(trim(gettype($value)) == 'string'){
  168. $q = mysqli_query(self::$connection,"ALTER TABLE `".$variable['table']."` ADD `".$key."` TEXT(65535) AFTER `".array_pop($arr2)."`");
  169. }
  170. if(gettype($value) == 'integer'){
  171. $q = mysqli_query(self::$connection,"ALTER TABLE `".$variable['table']."` ADD `".$key."` INT(128) AFTER `".array_pop($arr2)."`");
  172. }
  173. if(gettype($value) == 'double'||gettype($value) == 'float'){
  174. $q = mysqli_query(self::$connection,"ALTER TABLE `".$variable['table']."` ADD `".$key."` FLOAT(53) AFTER `".array_pop($arr2)."`");
  175. };
  176. }
  177. }
  178.  
  179. }else{
  180.  
  181. self::describe($variable['table']);
  182. $arr = [];
  183. $arr2 = [];
  184. $arr3 = [];
  185. foreach($variable as $key => $value){
  186. if(trim($key) != 'table'){
  187. $arr[$key] = $value;
  188. }
  189. }
  190. foreach (self::$arr as $key => $value) {
  191. if(trim(gettype($value)) == 'array'){
  192. foreach (self::$arr[$key] as $key => $value) {
  193. if($key == 'Field'){
  194. array_push($arr2,$value);
  195. }
  196. }
  197. }
  198. }
  199. foreach ($arr as $key => $value) {
  200. if(in_array($key, $arr2)){
  201.  
  202. }else{
  203. $arr3[$key] = $value;
  204. };
  205. }
  206. }
  207.  
  208. $insert = "INSERT INTO `".$variable['table']."`(";
  209. foreach ($variable as $key => $value) {
  210. if(trim($key) != 'table'){
  211. if(!array_key_exists($key,$arr3)){
  212.  
  213. $insert = $insert.''.$key.',';
  214. }
  215. }
  216. }
  217. $insert = substr($insert,0,-1);
  218. $insert = $insert.") VALUES(";
  219. foreach ($variable as $key => $value) {
  220. if(trim($key) != 'table'){
  221. if(!array_key_exists($key,$arr3)){
  222. if(trim(gettype($value)) == 'string'){
  223. $insert = $insert.'\''.$value.'\',';
  224. }else{
  225. $insert = $insert.''.$value.',';
  226. }
  227. }
  228. }
  229. }
  230. $insert = substr($insert,0,-1);
  231. $insert = $insert.')';
  232. foreach ($variable as $key => $value) {
  233. if(trim($key) != 'table'){
  234. if(trim(gettype($value)) == 'string'&&strlen($value) < 512){
  235. $variable[$key] = $key.' TEXT(512),
  236. ';
  237. }elseif(trim(gettype($value)) == 'string'&&strlen($value) > 512&&strlen($value) <= 1024){
  238. $variable[$key] = $key.' TEXT(1024),
  239. ';
  240. }elseif(trim(gettype($value)) == 'string'&&strlen($value) > 1024&&strlen($value) <= 2048){
  241. $variable[$key] = $key.' TEXT(2048),
  242. ';
  243. }elseif(trim(gettype($value)) == 'string'&&strlen($value) > 2048&&strlen($value) <= 4096){
  244. $variable[$key] = $key.' TEXT(4096),
  245. ';
  246. }elseif(trim(gettype($value)) == 'string'&&strlen($value) > 4096){
  247. $variable[$key] = $key.' TEXT(65535),
  248. ';
  249. }
  250. if(gettype($value) == 'integer'){
  251. $variable[$key] = $key.' INT(128),
  252. ';
  253. }
  254. if(gettype($value) == 'double'||gettype($value) == 'float'){
  255. $variable[$key] = $key.' FLOAT(53),
  256. ';
  257. }
  258. }
  259.  
  260. };
  261. $query = "
  262. CREATE TABLE ".$variable['table']."(
  263. id INT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  264. ";
  265. foreach ($variable as $key => $value) {
  266. if(trim($key) != 'table'){
  267. $query = $query.''.$value;
  268.  
  269. }
  270.  
  271. };
  272. $query = substr($query,0,-3);
  273. $query = $query.'
  274. )';
  275.  
  276. $q = mysqli_query(self::$connection,$query);
  277. $q = mysqli_query(self::$connection,$insert);
  278.  
  279.  
  280. }
  281. public function close(){
  282. mysqli_close(self::$connection);
  283. }
  284.  
  285. }
  286. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement