Advertisement
Guest User

Untitled

a guest
May 18th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. <?php
  2.  
  3. class User {
  4.  
  5. public $id;
  6. public $username;
  7. public $password;
  8. public $first_name;
  9. public $last_name;
  10.  
  11. public static function find_all_users() {
  12. return self::find_this_query("SELECT * FROM users");
  13. }
  14.  
  15. public static function find_user_by_id($user_id) {
  16. global $database;
  17. $the_result_array = self::find_this_query("SELECT * FROM users WHERE id = $user_id LIMIT 1");
  18. return !empty($the_result_array) ? array_shift($the_result_array) : false;
  19. }
  20.  
  21. public static function find_this_query($sql) {
  22. global $database;
  23. $result_set = $database->query($sql);
  24. $the_object_array = array();
  25.  
  26. while($row = mysqli_fetch_array($result_set)) {
  27. $the_object_array[] = self::instantiation($row);
  28. }
  29. return $the_object_array;
  30. }
  31.  
  32. public static function instantiation($the_record) {
  33. $the_object = new self;
  34. foreach ($the_record as $the_attribute => $value) {
  35. if($the_object->has_the_attribute($the_attribute)) {
  36. $the_object->$the_attribute = $value;
  37. }
  38. }
  39.  
  40. return $the_object;
  41. }
  42.  
  43. private function has_the_attribute($the_attribute) {
  44. $object_properties = get_object_vars($this);
  45. return array_key_exists($the_attribute, $object_properties);
  46. }
  47.  
  48. public static function verify_user($username, $password) {
  49. global $database;
  50.  
  51. $username = $database->escape_string($username);
  52. $username = $database->escape_string($password);
  53.  
  54. $sql = "SELECT * FROM users WHERE ";
  55. $sql .= "username = '{$username}' AND password = '{$password}' LIMIT 1";
  56.  
  57. $the_result_array = self::find_this_query($sql);
  58. return !empty($the_result_array) ? array_shift($the_result_array) : false;
  59. }
  60. }
  61.  
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement