Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. <?php
  2.  
  3. class User{
  4. private $dbServer = "sql12.freemysqlhosting.net";
  5. private $dbHost = "http://www.phpmyadmin.co/";
  6. private $dbUsername = "sql12168044";
  7. private $dbPassword = "";
  8. private $dbName = "sql12168044";
  9. private $userTbl = "users";
  10.  
  11. public function __construct(){
  12. if(!isset($this->db)){
  13. // Connect to the database
  14. $conn = new mysqli($this->dbHost, $this->dbServer, $this->dbUsername, $this->dbPassword, $this->dbName);
  15. if($conn->connect_error){
  16. die("Failed to connect with MySQL: " . $conn->connect_error);
  17. }else{
  18. $this->db = $conn;
  19. }
  20. }
  21. }
  22.  
  23. /*
  24. * Returns rows from the database based on the conditions
  25. * @param string name of the table
  26. * @param array select, where, order_by, limit and return_type conditions
  27. */
  28. public function getRows($conditions = array()){
  29. $sql = 'SELECT ';
  30. $sql .= array_key_exists("select",$conditions)?$conditions['select']:'*';
  31. $sql .= ' FROM '.$this->userTbl;
  32. if(array_key_exists("where",$conditions)){
  33. $sql .= ' WHERE ';
  34. $i = 0;
  35. foreach($conditions['where'] as $key => $value){
  36. $pre = ($i > 0)?' AND ':'';
  37. $sql .= $pre.$key." = '".$value."'";
  38. $i++;
  39. }
  40. }
  41.  
  42. if(array_key_exists("order_by",$conditions)){
  43. $sql .= ' ORDER BY '.$conditions['order_by'];
  44. }
  45.  
  46. if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
  47. $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit'];
  48. }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
  49. $sql .= ' LIMIT '.$conditions['limit'];
  50. }
  51.  
  52. $result = $this->db->query($sql);
  53.  
  54. if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
  55. switch($conditions['return_type']){
  56. case 'count':
  57. $data = $result->num_rows;
  58. break;
  59. case 'single':
  60. $data = $result->fetch_assoc();
  61. break;
  62. default:
  63. $data = '';
  64. }
  65. }else{
  66. if($result->num_rows > 0){
  67. while($row = $result->fetch_assoc()){
  68. $data[] = $row;
  69. }
  70. }
  71. }
  72. return !empty($data)?$data:false;
  73. }
  74.  
  75. /*
  76. * Insert data into the database
  77. * @param string name of the table
  78. * @param array the data for inserting into the table
  79. */
  80. public function insert($data){
  81. if(!empty($data) && is_array($data)){
  82. $columns = '';
  83. $values = '';
  84. $i = 0;
  85. if(!array_key_exists('created',$data)){
  86. $data['created'] = date("Y-m-d H:i:s");
  87. }
  88. if(!array_key_exists('modified',$data)){
  89. $data['modified'] = date("Y-m-d H:i:s");
  90. }
  91. foreach($data as $key=>$val){
  92. $pre = ($i > 0)?', ':'';
  93. $columns .= $pre.$key;
  94. $values .= $pre."'".$val."'";
  95. $i++;
  96. }
  97. $query = "INSERT INTO ".$this->userTbl." (".$columns.") VALUES (".$values.")";
  98. $insert = $this->db->query($query);
  99. return $insert?$this->db->insert_id:false;
  100. }else{
  101. return false;
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement