Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. <?php
  2.  
  3. class sqldatabase {
  4.  
  5. private $databaseHost;
  6. private $databaseName;
  7. private $databaseUser;
  8. private $databasePassword;
  9.  
  10. public $query;
  11.  
  12. public function __construct($databaseHost='', $databaseName='', $databaseUser='', $databasePassword='') {
  13.  
  14. $this->databaseHost = $databaseHost;
  15. $this->databaseName = $databaseName;
  16. $this->databaseUser = $databaseUser;
  17. $this->databasePassword = $databasePassword;
  18.  
  19. if ($this->databaseName != '' and $this->databaseUser != '' and $this->databasePassword != '') {
  20.  
  21. $this->connect();
  22.  
  23. }
  24.  
  25. }
  26.  
  27. private function connect() {
  28.  
  29. mysql_connect ($this->databaseHost, $this->databaseUser, $this->databasePassword); // Login to the Database
  30. mysql_select_db ($this->databaseName);
  31.  
  32. }
  33.  
  34. public function makesafe($variable) {
  35.  
  36. $variable = htmlspecialchars( $variable, ENT_QUOTES );
  37.  
  38. if( get_magic_quotes_gpc( ) ) {
  39.  
  40. $variable = stripslashes( $variable );
  41.  
  42. }
  43.  
  44. $variable = str_replace( "\"", "", $variable );
  45. //$variable = htmlentities( $variable );
  46. $variable = mysql_real_escape_string( $variable );
  47.  
  48. return $variable;
  49.  
  50. }
  51. public function query( $query ) {
  52.  
  53. $query = mysql_query($query);
  54. $this->query = $query;
  55. }
  56.  
  57. public function fetch ($query='') {
  58.  
  59. if ($query == '') {
  60.  
  61. $query = $this->query;
  62.  
  63. } else {
  64.  
  65. $query = mysql_query($query);
  66. $this->query = $query;
  67.  
  68. }
  69.  
  70. $result = mysql_fetch_object($query);
  71. return $result;
  72.  
  73. }
  74.  
  75. public function fetchAll ($query='') {
  76.  
  77. if ($query == '') {
  78.  
  79. $query = $this->query;
  80.  
  81. } else {
  82.  
  83. $query = mysql_query($query);
  84.  
  85. }
  86.  
  87. while ($result = mysql_fetch_object($query)) {
  88.  
  89. $fetch[] = $result;
  90.  
  91. }
  92.  
  93. unset($this->query);
  94.  
  95. return $this->array_to_object($fetch);
  96.  
  97.  
  98. }
  99.  
  100. public function count($query='') {
  101.  
  102. if ($query == '') {
  103.  
  104. $result = mysql_num_rows($this->query);
  105.  
  106. } else {
  107.  
  108. $query = mysql_query($query);
  109. $result = mysql_num_rows($query);
  110. }
  111.  
  112. return $result;
  113.  
  114. }
  115.  
  116. public function array_to_object($array = array()) {
  117.  
  118. if (is_array($array)) {
  119.  
  120. if (!empty($array)) {
  121.  
  122. $data = false;
  123.  
  124. foreach ($array as $akey => $aval) {
  125.  
  126. $data -> {$akey} = $aval;
  127.  
  128. }
  129.  
  130. return $data;
  131.  
  132. }
  133.  
  134. } else {
  135.  
  136. return $array;
  137.  
  138. }
  139.  
  140. }
  141.  
  142. public function getColumnType ($table, $column) {
  143.  
  144. $query = mysql_query("SELECT $column FROM $table");
  145.  
  146. if (!$query) {
  147.  
  148. return false;
  149.  
  150. }
  151.  
  152. $field = mysql_field_type($query, 0);
  153.  
  154. if (!$field) {
  155.  
  156. mysql_free_result($field);
  157. return false;
  158.  
  159. }
  160.  
  161. mysql_free_result($query);
  162. return $field;
  163.  
  164. }
  165.  
  166. public function insert ( $table, $array='' ) {
  167.  
  168. if ($array == '') {
  169.  
  170. $query = $table;
  171. $query = mysql_query ( $query );
  172.  
  173. $id = mysql_insert_id();
  174.  
  175. if ($id == 0) {
  176.  
  177. return true;
  178.  
  179. } else {
  180.  
  181. return $id;
  182.  
  183. }
  184.  
  185. } else {
  186.  
  187. if (is_array($array)) {
  188.  
  189. if (empty($array)) {
  190.  
  191. return false;
  192.  
  193. } else {
  194.  
  195. $cols = '(';
  196. $values = '(';
  197.  
  198. foreach ($array as $key=>$value) { // iterate values to input
  199.  
  200. $cols .= "`$key`, ";
  201. $values .= "'$value', ";
  202.  
  203. }
  204.  
  205.  
  206. $cols = rtrim($cols, ', ').')';
  207. $values = rtrim($values, ', ').')';
  208. $query = $this->insert("INSERT INTO `$table` $cols VALUES $values");
  209. return $query;
  210.  
  211. }
  212.  
  213.  
  214. } else {
  215.  
  216. return false;
  217.  
  218. }
  219.  
  220. }
  221.  
  222. }
  223.  
  224. public function delete ($table, $column, $value='') {
  225.  
  226. if ($value == '' and is_array($column) == true) {
  227.  
  228. foreach ($column as $key => $value) {
  229.  
  230. $by .= "`$key` = '$value' and ";
  231.  
  232. }
  233.  
  234. $by = rtrim($by, ' and ');
  235.  
  236. $query = mysql_query("DELETE FROM `$table` where $by");
  237.  
  238.  
  239. } else {
  240.  
  241. $query = mysql_query("DELETE FROM `$table` where `$column` = '$value'");
  242.  
  243. if (!$query) {
  244.  
  245. return false;
  246.  
  247. }
  248.  
  249. return true;
  250.  
  251. }
  252.  
  253. }
  254.  
  255. public function update ($table, $where, $wvalue, $array='') {
  256.  
  257. if (is_array($array)) {
  258.  
  259. if (empty($array)) {
  260.  
  261. return false;
  262.  
  263. } else {
  264. foreach ($array as $key=>$value) { // iterate values to input
  265.  
  266. $values .= "`$key` = '$value', ";
  267. }
  268.  
  269.  
  270. $values = rtrim($values, ', ');
  271. $query = mysql_query("UPDATE `$table` SET $values WHERE `$where` = '$wvalue'");
  272. return $query;
  273.  
  274. }
  275.  
  276. }
  277.  
  278. }
  279.  
  280. public function updateTwo ($table, $whereOne, $isOne, $whereTwo, $isTwo, $array='') {
  281.  
  282. if (is_array($array)) {
  283.  
  284. if (empty($array)) {
  285.  
  286. return false;
  287.  
  288. } else {
  289. foreach ($array as $key=>$value) { // iterate values to input
  290.  
  291. $values .= "`$key` = '$value', ";
  292. }
  293.  
  294.  
  295. $values = rtrim($values, ', ');
  296. $query = mysql_query("UPDATE `$table` SET $values WHERE `$whereOne` = '$isOne' and `$whereTwo` = '$isTwo'");
  297. return $query;
  298.  
  299. }
  300.  
  301. }
  302.  
  303. }
  304.  
  305. }
  306.  
  307. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement