# [SOLVED] PHP MySQL Login / Register



## owain (Oct 26, 2009)

Ive been trying to code a login and registration form using PHP and MySQL but cannot get it to work at all, So far i have two files db.PHP and User.PHP

When i run the db.php file using EasyPHP 12.1 it runs with no errors but when i run the User.php i get the following error message:

Parse error: syntax error, unexpected ';' in C:\Program Files(x86)\EasyPHP-12.1\www\login_FormTest\User.php on line 80

User.PHP

```
<?php
	// User class to store user information for logged user
class User {
	private $uid; // User id
	private $fields; // Other records on file
	
	// Initialise our user object
	public function __construct() {
		$this->uid = null;
		$this->fields = array('username' => '', 'password' => '', 'emailAddr' => '', 'isActive' => false);
	}
	
	// Overide magic method to retrieve properties
	public function __get($field) {
		if($field == 'userId') {
			return $this-uid;
		}else{
			return $this->fields[$field];
		}
	}
	
	// Overide magic method to set properties
	public function __set($field, $value){
		if(array_key_exists($field, $this->fields)) {
			$this->fields[$field] = $value;
		}
	}
	
	// Return if username is valid format
	public static function validateUsername($username) {
		return preg_match('/^[A-Z0-9]{2,20}$/i', $username);
	}
	
	// Return if email is valid format
	public static function validateEmailAddr($email) {
		return filter_var($email, FILTER_VALIDATE_EMAIL);
	}
	
	// Return an object populated based on a users ID
	public static function getById($user_id) {
		$user = new User();
		$query = sprintf('SELECT USERNAME, PASSWORD, EMAIL_ADDR, IS_ACTIVE FROM %sUSER WHERE USER_ID = %d', DB_TBL_PREFIX, $user_id);
		$result = mysql_query($query, $GLOBALS['DB']);
		if(mysql_num_rows($result)) {
			$row = mysql_fetch_assoc($result);
			$user->username = $row['USERNAME'];
			$user->password = $row['PASSWORD'];
			$user->emailAddr = $row['EMAIL_ADDR'];
			$user->isActive = $row['IS_ACTIVE'];
			$user->uid = $user_id;
		}
		mysql_free_result($result);
		return $user;
	}
	
	// Return an object populated based on a username
	public static function getByUsername($username) {
		$user = new User();
		$query = sprintf('SELECT USER_ID, PASSWORD, EMAIL_ADDR, IS_ACTIVE FROM %sUSER WHERE USERNAME = "%s"', DB_TBL_PREFIX, mysql_real_escape_string($username, $GLOBALS['DB']));
		$result = mysql_query($query, $GLOBALS['DB']);
		if(mysql_num_rows($result)) {
			$row = mysql_fetch_assoc($result);
			$user->username = $username;
			$user->password = $row['PASSWORD']; 
			$user->emailAddr = $row['EMAIL_ADDR'];
			$user->isActive = $row['IS_ACTIVE'];
			$user->uid = $row['USER_ID'];
		}
		mysql_free_result($result);
		return $user;
	}
	
	// Save the record to the database
	public function save() {
		if($this->uid {
			$query = sprintf('UPDATE %sUSER SET USERNAME = "%s", PASSWORD = "%s", EMAIL_ADDR = "%s", IS_ACTIVE = %d WHERE USER_ID = %d', DB_TBL_PREFIX,
			mysql_real_escape_string($this->username, $GLOBALS['DB']),
			mysql_real_escape_string($this->password, $GLOBALS['DB']),
			mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
			$this->isActive, $this->userId);
			return mysql_query($query, $GLOBALS['DB']);		
		}else{
		// If the user has not register yet		
			$query = sprintf('INSERT INTO $USER (USERNAME, PASSWORD, EMAIL_ADDR, IS_ACTIVE) VALUES ("%s", "%s", "%s", %d)', DB_TBL_PREFIX,
			mysql_real_escape_string($this->username, $GLOBALS['DB']),
			mysql_real_escape_string($this->password, $GLOBALS['DB']),
			mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
			$this->isActive);
			if(mysql_query($query, $GLOBALS['DB'])) {
				$this->uid = mysql_insert_id($GLOBALS['DB']);
				return true;
			}		
	}
}
?>
```
DB.PHP:

```
<?php
// Database connection and schema constants

define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_SCHEMA', 'login_tutorial');
define('DB_TBL_PREFIX', 'BHZ_');

// Establish a connection to the database
if(!$GLOBALS['DB'] = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)) {
	die('Error: Unable to connect to the database');
}
if (!mysql_select_db(DB_SCHEMA, $GLOBALS['DB'])) {
	mysql_close($GLOBALS['DB']);
	die('Error: Unable to select database schema');
}

?>
```
From what i can see though there is no unexpected ; on line 80 or any of the lines for that matter. Can anyone see what ive done wrong? If not can you point me in the direction of a good tutorial to help me with this. any help would be much appreciated, thankyou.


----------



## Fjandr (Sep 26, 2012)

*Re: PHP MySQL Login / Register*

On line 75

```
if($this->uid {
```
the ( is unpaired.


----------



## wmorri (May 29, 2008)

*Re: PHP MySQL Login / Register*

Good find.


----------



## owain (Oct 26, 2009)

*Re: PHP MySQL Login / Register*

Oh yeah haha cheers, Great find


----------



## owain (Oct 26, 2009)

*Re: PHP MySQL Login / Register*

Was also missing a } at the end again thanks


----------



## Fjandr (Sep 26, 2012)

Oh yep. Missed that the class declaration was never closed.

Good luck with the rest of it.


----------

