Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. <?php
  2.  
  3. class database {
  4. public $lastInsertId;
  5. private $host;
  6. private $username;
  7. private $password;
  8. private $database;
  9.  
  10. private $connection;
  11. private $legacyConnection;
  12.  
  13. public function __construct($host, $username, $password, $database) {
  14. $this->host = $host;
  15. $this->username = $username;
  16. $this->password = $password;
  17. $this->database = $database;
  18. $this->connection = new mysqli($this->host, $this->username, $this->password, $this->database);
  19.  
  20. if($this->connection->connect_errno > 0) {
  21. die('Unable to connect with Mysql database...');
  22. }
  23.  
  24. $this->legacyConnection = mysql_connect($this->host, $this->username, $this->password);
  25.  
  26. }
  27.  
  28. public function __destruct() {
  29. $this->connection->close();
  30.  
  31. if($this->legacyConnection) {
  32. @mysql_close($this->legacyConnection);
  33. }
  34. }
  35.  
  36. /** $database->query
  37. * Works like runQuery except it'll return an array of objects or a single object if only one row is collected. If $forceArrayReturn itll always return an array
  38. */
  39. public function query($queryString, $forceArrayReturn = false) {
  40.  
  41. $result = $this->connection->query($queryString);
  42.  
  43. //bail if query fail. return true if bool was returned
  44. if(!$result) {
  45. return false;
  46. }elseif($result === true) {
  47. return true;
  48. }
  49.  
  50. //collect rows
  51. while($row = $result->fetch_assoc()){
  52. $return[] = $row;
  53. }
  54.  
  55. if(count(@$return) === 1) {
  56. $obj = new stdClass();
  57. foreach($return[0] as $key => $value) {
  58. $obj->$key = $value;
  59. }
  60.  
  61. if($forceArrayReturn) {
  62. return array($obj);
  63. }
  64.  
  65. return $obj;
  66. }elseif(sizeof(@$return) >= 2){
  67. foreach($return as $row) {
  68. $obj = new stdClass();
  69. foreach($row as $key => $value) {
  70. $obj->$key = $value;
  71. }
  72. $returnRows[] = $obj;
  73. }
  74. return $returnRows;
  75. }
  76. }
  77.  
  78. public function runQuery($query) {
  79.  
  80. mysql_select_db($this->database, $this->legacyConnection);
  81.  
  82. $result = mysql_query($query);
  83. return $result;
  84. }
  85.  
  86. public function dbPrepare($string, $case = "default") {
  87. $string = mysql_real_escape_string($string);
  88.  
  89. if (empty($string)) {
  90. $string = "NULL";
  91. } else {
  92. switch ($case) {
  93. case "upper":
  94. $string = strtoupper($string);
  95. break;
  96. case "lower":
  97. $string = strtolower($string);
  98. break;
  99. case "ucfirst":
  100. $string = strtolower($string);
  101. $string = ucfirst($string);
  102. break;
  103. case "lcfirst":
  104. $string = lcfirst($string);
  105. break;
  106. case "ucwords":
  107. $string = strtolower($string);
  108. $string = ucwords($string);
  109. break;
  110. default:
  111. break;
  112. }
  113. $string = "'" . $string . "'";
  114. }
  115.  
  116. return $string;
  117. }
  118.  
  119. function checkEmpty($string) {
  120. $string = mysql_real_escape_string($string);
  121.  
  122. if (empty($string)) {
  123. $string = "'%'";
  124. return $string;
  125. } else {
  126. $string = "'$string'";
  127. return $string;
  128. }
  129. }
  130. }
  131.  
  132. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement