Guest User

Untitled

a guest
Jul 20th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. <?php
  2. /**
  3. * @author Jarod B
  4. * users class
  5. * Used to access the uses table
  6. */
  7. class users {
  8.  
  9. /**
  10. * create_table()
  11. * Grabs the user's name from the 'users' table
  12. * @return mysql_query($sql)
  13. */
  14. public function create_table() {
  15. $sql = "
  16. CREATE TABLE IF NOT EXISTS users (
  17. users_id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  18. users_username VARCHAR(12) NOT NULL,
  19. users_password VARCHAR(24) NOT NULL
  20. );
  21. ";
  22.  
  23. return mysql_query($sql);
  24. }
  25.  
  26. /**
  27. * create_new_user()
  28. * Grabs the user's name from the 'users' table
  29. * @param string $user the user's username
  30. * @param string $pass the user's password
  31. * @return creates a new user row if username don't exist
  32. */
  33. public function create_new_user($user, $pass) {
  34. if(!strpos($this->get_username($user), "does not exist")) {
  35. // Check if username is taken
  36. return "'$user' is taken, sorry";
  37. } else {
  38. $sql = "INSERT INTO users (users_username, users_password) VALUES('$user', '$pass')";
  39. mysql_query($sql);
  40.  
  41. return "'$user' has been created!";
  42. }
  43. }
  44.  
  45. /**
  46. * get_username()
  47. * Inserts a new row into the users table, this is good for logins and registration
  48. * @param string $user Existing username of the users table
  49. */
  50. public function get_username($user) {
  51. $sql = "SELECT users_username FROM users WHERE users_username='$user'";
  52. $query = mysql_query($sql);
  53.  
  54. $count = mysql_num_rows($query); // Searching for username, should return 1 if username is found
  55.  
  56. if($count >= 1) {
  57. while($row = mysql_fetch_assoc($query)) {
  58. $users_username = $row['users_username'];
  59.  
  60. return $users_username;
  61. }
  62. } else { // Username does not exist then
  63. return -1;
  64. }
  65. }
  66.  
  67. /**
  68. * get_password()
  69. * Grabs the user's password from the 'users' table
  70. * @param string $pass Password of the users table
  71. */
  72. public function get_password($pass) {
  73. $sql = "SELECT users_password FROM users WHERE users_password='$pass'";
  74. $query = mysql_query($sql);
  75.  
  76. $count = mysql_num_rows($query); // Searching for password, should return 1 if password is found
  77.  
  78. if($count >= 1) {
  79. return "$pass found!";
  80. } else { // Username does not exist then
  81. return -1;
  82. }
  83. }
  84.  
  85. /**
  86. * row_count()
  87. * Returns the total amount of EXISTING users
  88. * @return returns the total amount of usernames in the users table with an integer value
  89. */
  90. public function users_count() {
  91. $sql = "SELECT users_username FROM users";
  92. $query = mysql_query($sql);
  93.  
  94. return (int)mysql_num_rows($query);
  95. }
  96. }
  97. ?>
Add Comment
Please, Sign In to add comment