Advertisement
Guest User

code that doesn't work

a guest
Feb 17th, 2022
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.27 KB | None | 0 0
  1. <?php
  2. if (!defined("_VALID_PHP"))
  3. die('Direct access to this location is not allowed.');
  4.  
  5. class Database
  6. {
  7. private $server = "localhost";
  8. private $user = "admin";
  9. private $pass = "lol123";
  10. private $database = "czaisz_baze";
  11. public $error = "error.txt";
  12. public $errno = 0;
  13. protected $affected_rows = 0;
  14. protected $query_counter = 0;
  15. protected $link_id = 0;
  16. protected $query_id = 0;
  17. /**
  18. * Database::__construct()
  19. *
  20. * @param mixed $server
  21. * @param mixed $user
  22. * @param mixed $pass
  23. * @param mixed $database
  24. * @return
  25. */
  26. function __construct($server, $user, $pass, $database)
  27. {
  28. $this->server = $server;
  29. $this->user = $user;
  30. $this->pass = $pass;
  31. $this->database = $database;
  32.  
  33. }
  34.  
  35. /**
  36. * Database::connect()
  37. * Connect and select database using vars above
  38. * @return
  39. */
  40. function connect()
  41. {
  42. $this->link_id = $this->connect_db($this->server, $this->user, $this->pass);
  43.  
  44. if (!$this->link_id)
  45. $this->error("<div style='text-align:center'>"
  46. . "<span style='padding: 5px; border: 1px solid #999; background-color:#EFEFEF;"
  47. . "font-family: Verdana; font-size: 11px; margin-left:auto; margin-right:auto'>"
  48. . "<b>Database Error:</b>Connection to Database " . $this->database . " Failed</span></div>");
  49.  
  50. if (!$this->select_db($this->database, $this->link_id))
  51. $this->error("<div style='text-align:center'>"
  52. . "<span style='padding: 5px; border: 1px solid #999; background-color: #EFEFEF;"
  53. . "font-family: Verdana; font-size: 11px; margin-left:auto; margin-right:auto'>"
  54. . "<b>Database Error:</b>mySQL database (" . $this->database . ")cannot be used</span></div>");
  55.  
  56. $this->query("SET NAMES 'utf8'", $this->link_id);
  57. $this->query("SET CHARACTER SET 'utf8'", $this->link_id);
  58. $this->query("SET CHARACTER_SET_CONNECTION=utf8", $this->link_id);
  59. $this->query("SET SQL_MODE = ''", $this->link_id);
  60.  
  61. unset($this->password);
  62. }
  63.  
  64. /**
  65. * Database::connect_db()
  66. *
  67. * @param mixed $server
  68. * @param mixed $user
  69. * @param mixed $pass
  70. * @return
  71. */
  72. function connect_db($server, $user, $pass)
  73. {
  74. return mysqli_connect($server, $user, $pass);
  75. }
  76.  
  77. /**
  78. * Database::select_db()
  79. *
  80. * @param mixed $database
  81. * @param mixed $link_id
  82. * @return
  83. */
  84. function select_db($link_id, $database,)
  85. {
  86. return mysqli_select_db($link_id, string $database);
  87. }
  88.  
  89.  
  90. /**
  91. * Database::query()
  92. * Executes SQL query to an open connection
  93. * @param mixed $sql
  94. * @return (query_id)
  95. */
  96. function query($sql)
  97. {
  98. if (trim($sql != "")) {
  99. $this->query_counter++;
  100. $this->query_show .= stripslashes($sql) . "<hr size='1' />";
  101. $this->query_id = mysqli_query($sql, $this->link_id);
  102.  
  103. $this->last_query = $sql . '<br />';
  104. }
  105.  
  106. if (!$this->query_id)
  107. $this->error("mySQL Error on Query : " . $sql);
  108.  
  109. return $this->query_id;
  110.  
  111. }
  112.  
  113. /**
  114. * Database::first()
  115. * Fetches the first row only, frees resultset
  116. * @param mixed $string
  117. * @return array
  118. */
  119. function first($string)
  120. {
  121. $query_id = $this->query($string);
  122. $record = $this->fetch($query_id);
  123. $this->free($query_id);
  124.  
  125. return $record;
  126. }
  127.  
  128. /**
  129. * Database::fetch()
  130. * Fetches and returns results one line at a time
  131. * @param integer $query_id
  132. * @return array
  133. */
  134. function fetch($query_id = -1)
  135. {
  136. if ($query_id != -1)
  137. $this->query_id = $query_id;
  138.  
  139. if (isset($this->query_id)) {
  140. $record = mysqli_fetch_array($this->query_id, mysqli_ASSOC);
  141. } else
  142. $this->error("Invalid query_id: <b>" . $this->query_id . "</b>. Records could not be fetched.");
  143.  
  144. return $record;
  145. }
  146.  
  147. /**
  148. * Database::fetch_all()
  149. * Returns all the results
  150. * @param mixed $sql
  151. * @return assoc array
  152. */
  153. function fetch_all($sql)
  154. {
  155. $query_id = $this->query($sql);
  156. $record = array();
  157.  
  158. while ($row = $this->fetch($query_id, $sql)) :
  159. $record[] = $row;
  160. endwhile;
  161.  
  162. $this->free($query_id);
  163.  
  164. return $record;
  165. }
  166.  
  167. /**
  168. * Database::free()
  169. * Frees the resultset
  170. * @param integer $query_id
  171. * @return query_id
  172. */
  173. function free($query_id = -1)
  174. {
  175. if ($query_id != -1)
  176. $this->query_id = $query_id;
  177.  
  178. return mysqli_free_result($this->query_id);
  179. }
  180.  
  181. /**
  182. * Database::insert()
  183. * Insert query with an array
  184. * @param mixed $table
  185. * @param mixed $data
  186. * @return id of inserted record, false if error
  187. */
  188. function insert($data, $table = null)
  189. {
  190. global $core;
  191. if ($table === null or empty($data) or !is_array($data)) {
  192. $this->error("Invalid array for table: <b>".$table."</b>.");
  193. return false;
  194. }
  195. $q = "INSERT INTO `" . $table . "` ";
  196. $v = '';
  197. $k = '';
  198.  
  199. foreach ($data as $key => $val) :
  200. $k .= "`$key`, ";
  201. if (strtolower($val) == 'null')
  202. $v .= "NULL, ";
  203. elseif (strtolower($val) == 'now()')
  204. $v .= "NOW(), ";
  205. elseif (strtolower($val) == 'tzdate')
  206. $v .= "DATE_ADD(NOW(),INTERVAL " . $core->timezone . " HOUR), ";
  207. else
  208. $v .= "'" . $this->escape($val) . "', ";
  209. endforeach;
  210.  
  211. $q .= "(" . rtrim($k, ', ') . ") VALUES (" . rtrim($v, ', ') . ");";
  212.  
  213. if ($this->query($q)) {
  214. return $this->insertid();
  215. } else
  216. return false;
  217. }
  218.  
  219. /**
  220. * Database::update()
  221. * Update query with an array
  222. * @param mixed $table
  223. * @param mixed $data
  224. * @param string $where
  225. * @return query_id
  226. */
  227. function update($data, $where = '1', $table = null)
  228. {
  229. global $core;
  230. if ($table === null or empty($data) or !is_array($data)) {
  231. $this->error("Invalid array for table: <b>" . $table . "</b>.");
  232. return false;
  233. }
  234.  
  235. $q = "UPDATE `" . $table . "` SET ";
  236. foreach ($data as $key => $val) :
  237. if (strtolower($val) == 'null')
  238. $q .= "`$key` = NULL, ";
  239. elseif (strtolower($val) == 'now()')
  240. $q .= "`$key` = NOW(), ";
  241. elseif (strtolower($val) == 'tzdate')
  242. $q .= "`$key` = DATE_ADD(NOW(),INTERVAL " . $core->timezone . " HOUR), ";
  243. elseif (strtolower($val) == 'default()')
  244. $q .= "`$key` = DEFAULT($val), ";
  245. elseif(preg_match("/^inc\((\-?\d+)\)$/i",$val,$m))
  246. $q.= "`$key` = `$key` + $m[1], ";
  247. else
  248. $q .= "`$key`='" . $this->escape($val) . "', ";
  249. endforeach;
  250. $q = rtrim($q, ', ') . ' WHERE ' . $where . ';';
  251.  
  252. return $this->query($q);
  253. }
  254.  
  255. /**
  256. * Database::delete()
  257. * Delete records
  258. * @param mixed $table
  259. * @param string $where
  260. * @return
  261. */
  262. function delete($table, $where = '')
  263. {
  264. $q = !$where ? 'DELETE FROM ' . $table : 'DELETE FROM ' . $table . ' WHERE ' . $where;
  265. return $this->query($q);
  266. }
  267.  
  268. /**
  269. * Database::insert_id()
  270. * Returns last inserted ID
  271. * @param integer $query_id
  272. * @return
  273. */
  274. function insertid()
  275. {
  276. return mysqli_insert_id($this->link_id);
  277. }
  278.  
  279. /**
  280. * Database::affected()
  281. * Returns the number of affected rows
  282. * @param integer $query_id
  283. * @return
  284. */
  285. function affected() {
  286. return mysqli_affected_rows($this->link_id);
  287. }
  288.  
  289. /**
  290. * Database::numrows()
  291. *
  292. * @param integer $query_id
  293. * @return
  294. */
  295. function numrows($query_id = -1)
  296. {
  297. if ($query_id != -1)
  298. $this->query_id = $query_id;
  299.  
  300. $this->num_rows = mysqli_num_rows($this->query_id);
  301. return $this->num_rows;
  302. }
  303.  
  304. /**
  305. * Database::fetchrow()
  306. * Fetches one row of data
  307. * @param integer $query_id
  308. * @return fetched row
  309. */
  310. function fetchrow($query_id = -1)
  311. {
  312. if ($query_id != -1)
  313. $this->query_id = $query_id;
  314.  
  315. $this->fetch_row = mysqli_fetch_row($this->query_id);
  316. return $this->fetch_row;
  317. }
  318.  
  319. /**
  320. * Database::numfields()
  321. *
  322. * @param integer $query_id
  323. * @return
  324. */
  325. function numfields($query_id = -1)
  326. {
  327. if ($query_id != -1)
  328. $this->query_id = $query_id;
  329.  
  330. $this->num_fields = mysqli_num_fields($this->query_id);
  331. return $this->num_fields;
  332. }
  333.  
  334. /**
  335. * Database::show()
  336. *
  337. * @return
  338. */
  339. function show()
  340. {
  341. return "<br /><br /><b> Debug Mode - All Queries :</b><hr size='1' /> " . $this->query_show . "<br />";
  342. }
  343.  
  344. /**
  345. * Database::pre()
  346. *
  347. * @return
  348. */
  349. function pre($arr)
  350. {
  351. print '<pre>' . print_r($arr, true) . '</pre>';
  352. }
  353.  
  354.  
  355. /**
  356. * Database::escape_()
  357. *
  358. * @param mixed $string
  359. * @param bool $do
  360. * @return Database::quote()
  361. */
  362. function escape_($string)
  363. {
  364. return mysqli_real_escape_string($string, $this->link_id);
  365. }
  366.  
  367. /**
  368. * Database::getDB()
  369. *
  370. * @return
  371. */
  372. function getDB()
  373. {
  374. return $this->database;
  375. }
  376.  
  377. /**
  378. * Database::getServer()
  379. *
  380. * @return
  381. */
  382. function getServer()
  383. {
  384. return $this->server;
  385. }
  386.  
  387. /**
  388. * Database::error()
  389. * Output error message
  390. * @param mixed $msg
  391. * @return
  392. */
  393. function error($msg = '')
  394. {
  395. global $DEBUG, $_SERVER;
  396. if ($this->link_id > 0) {
  397. $this->error_desc = mysqli_error($this->link_id);
  398. $this->error_no = mysqli_errno($this->link_id);
  399. } else {
  400. $this->error_desc = mysqli_error();
  401. $this->error_no = mysqli_errno();
  402. }
  403.  
  404. $the_error = "<div style=\"background-color:#FFF; border: 3px solid #999; padding:10px\">";
  405. $the_error .= "<b>mySQL WARNING!</b><br />";
  406. $the_error .= "DB Error: $msg <br /> More Information: <br />";
  407. $the_error .= "<ul>";
  408. $the_error .= "<li> Mysql Error : " . $this->error_no . "</li>";
  409. $the_error .= "<li> Mysql Error no # : " . $this->error_desc . "</li>";
  410. $the_error .= "<li> Date : " . date("F j, Y, g:i a") . "</li>";
  411. $the_error .= "<li> Referer: " . isset($_SERVER['HTTP_REFERER']) . "</li>";
  412. $the_error .= "<li> Script: " . $_SERVER['REQUEST_URI'] . "</li>";
  413. $the_error .= '</ul>';
  414. $the_error .= '</div>';
  415. if ($DEBUG)
  416. echo $the_error;
  417. die();
  418. }
  419.  
  420. }
  421. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement