Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author Jarod B
- * users class
- * Used to access the uses table
- */
- class users {
- /**
- * create_table()
- * Grabs the user's name from the 'users' table
- * @return mysql_query($sql)
- */
- public function create_table() {
- $sql = "
- CREATE TABLE IF NOT EXISTS users (
- users_id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
- users_username VARCHAR(12) NOT NULL,
- users_password VARCHAR(24) NOT NULL
- );
- ";
- return mysql_query($sql);
- }
- /**
- * create_new_user()
- * Grabs the user's name from the 'users' table
- * @param string $user the user's username
- * @param string $pass the user's password
- * @return creates a new user row if username don't exist
- */
- public function create_new_user($user, $pass) {
- if(!strpos($this->get_username($user), "does not exist")) {
- // Check if username is taken
- return "'$user' is taken, sorry";
- } else {
- $sql = "INSERT INTO users (users_username, users_password) VALUES('$user', '$pass')";
- mysql_query($sql);
- return "'$user' has been created!";
- }
- }
- /**
- * get_username()
- * Inserts a new row into the users table, this is good for logins and registration
- * @param string $user Existing username of the users table
- */
- public function get_username($user) {
- $sql = "SELECT users_username FROM users WHERE users_username='$user'";
- $query = mysql_query($sql);
- $count = mysql_num_rows($query); // Searching for username, should return 1 if username is found
- if($count >= 1) {
- while($row = mysql_fetch_assoc($query)) {
- $users_username = $row['users_username'];
- return $users_username;
- }
- } else { // Username does not exist then
- return -1;
- }
- }
- /**
- * get_password()
- * Grabs the user's password from the 'users' table
- * @param string $pass Password of the users table
- */
- public function get_password($pass) {
- $sql = "SELECT users_password FROM users WHERE users_password='$pass'";
- $query = mysql_query($sql);
- $count = mysql_num_rows($query); // Searching for password, should return 1 if password is found
- if($count >= 1) {
- return "$pass found!";
- } else { // Username does not exist then
- return -1;
- }
- }
- /**
- * row_count()
- * Returns the total amount of EXISTING users
- * @return returns the total amount of usernames in the users table with an integer value
- */
- public function users_count() {
- $sql = "SELECT users_username FROM users";
- $query = mysql_query($sql);
- return (int)mysql_num_rows($query);
- }
- }
- ?>
Add Comment
Please, Sign In to add comment