Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Meshaq
  5. * Date: 3/3/14
  6. * Time: 4:00 PM
  7. */
  8.  
  9. namespace Entity;
  10.  
  11.  
  12. class Database {
  13. protected $dbHost;
  14. protected $dbName;
  15. protected $dbUser;
  16. protected $dbPassword;
  17. protected $dbConnection;
  18. protected $dbTable;
  19. protected $dbQuery;
  20. protected $dbConnect;
  21.  
  22. public function set(){
  23. $this->dbHost = (DB_HOST);
  24. $this->dbName = (DB_NAME);
  25. $this->dbUser = (DB_USER);
  26. $this->dbPassword = (DB_PASSWORD);
  27. }
  28. public function open(){
  29. $this->set();
  30. $conn = mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword);
  31. $con = mysql_select_db($this->dbName,$conn);
  32. if ($con){
  33. $this->dbConnect = true;
  34. $this->dbConnection = $con;
  35. }else{
  36. echo mysql_error()."<br>";
  37. }
  38. }
  39.  
  40.  
  41. //close database connection
  42. public function close(){
  43. $closed = mysql_close();
  44. if ($closed){
  45. $this->dbConnect = false;
  46. }else{
  47. $this->dbConnect = true;
  48. echo mysql_error($this->dbConnection);
  49. }
  50.  
  51. }
  52.  
  53. public function query($query){
  54. $this->dbQuery = $query;
  55. }
  56.  
  57.  
  58. public function persist(){
  59. $this->open();
  60. if($this->dbQuery != NULL || $this->dbQuery ==""){
  61. // $this->table($this->dbName);
  62. $doQuery = mysql_query($this->dbQuery) or die(mysql_error());
  63. if($doQuery){
  64. return $doQuery;
  65. }else{
  66. return "Error".mysql_errno()."".mysql_error();
  67. }
  68.  
  69. }
  70. }
  71.  
  72. public function fetch($arr){
  73. return mysql_fetch_assoc($arr);
  74. }
  75.  
  76. public function flush(){
  77. unset($this);
  78. }
  79.  
  80. public function getEntity($entity){
  81. $this->query("SELECT * FROM ".$entity."");
  82. $result = $this->persist();
  83. $entity = $this->fetch($result);
  84.  
  85. return $entity;
  86. }
  87. public function getRawEntity($entity){
  88. $this->query("SELECT * FROM ".$entity."");
  89. $entity = $this->persist();
  90.  
  91. return $entity;
  92. }
  93.  
  94. public function searchEntity($entity,$term){
  95. $searchphrase = $term;
  96. $table = $entity;
  97. $sql_search = "select * from ".$table." where ";
  98. $sql_search_fields = Array();
  99. $sql = "SHOW COLUMNS FROM ".$table;
  100. $rs = mysql_query($sql);
  101. while($r = mysql_fetch_array($rs)){
  102. $colum = $r[0];
  103. $sql_search_fields[] = $colum." like('%".$searchphrase."%')";
  104. }
  105.  
  106. $sql_search .= implode(" OR ", $sql_search_fields);
  107. $rs2 = $this->query($sql_search);
  108. $result = $this->persist();
  109. if ($this->rows($result)>0){
  110. return $result;
  111. }else{
  112. return false;
  113. }
  114. }
  115. public function getEntityById($entity,$id){
  116. $this->query("SELECT * FROM ".$entity." WHERE id='$id'");
  117. $result = $this->persist();
  118. $entity = $this->fetch($result);
  119.  
  120. return $entity;
  121. }
  122.  
  123. public function getEntityBy($entity,$col,$term){
  124. $this->query("SELECT * FROM ".$entity." WHERE ".$col."='$term'");
  125. $result = $this->persist();
  126. $entity = $this->fetch($result);
  127.  
  128. return $entity;
  129. }
  130. public function getRawEntityBy($entity,$col,$term){
  131. $this->query("SELECT * FROM ".$entity." WHERE ".$col."='$term'");
  132. $result = $this->persist();
  133.  
  134. return $result;
  135. }
  136.  
  137. public function getEntityDetail($entity,$id,$col){
  138. $this->query("SELECT * FROM ".$entity." WHERE id='$id'");
  139. $result = $this->persist();
  140.  
  141. while($entity = $this->fetch($result)){
  142. return $entity[$col];
  143. }
  144. }
  145. public function getId(){
  146. return mysql_insert_id();
  147. }
  148.  
  149. public function rows($entities){
  150. return mysql_num_rows($entities);
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement