I am beginner and this is my first project (happy face). I am trying to make log in systen to learn php and mySQL. At first everything was working so and then i decided to rewrite everything in Object oriented code and then it is not working. Since it is not syntax error it is hard to find for me, but i know that problem is in mySQL query, because it is empty.
Problem: It doesn't work and it doesn't set $errorLevel and there is no mysql_error() report
here is the class
private $queryResults; // will be used for checking user name and password
* 1 if user is sucessfuly loged in
* -1 if user name is not in db
* -2 if cannot connect to db
public function try_log_in($user_name, $password)
if($this->is_userName_in_db() && $this->is_password_correct())
//-------------------------Under the hud-----------------------//
//escapes all inputs and sets object`s parameters
private function setup_parameters($user_name, $password)
$this->userName = mysql_real_escape_string ($user_name);
$this->password = mysql_real_escape_string ($password);
$query = "
WHERE userName = '$this->userName' ";
$this->queryResults = mysql_query($query);
/*Checks if given user name is db
* 1 if it is in db
* -1 if it is not in db
* -2 if cannot connect to db
private function is_userName_in_db()
$this->errorLevel = -2;
if(mysql_num_rows( $this->queryResults) == 0)
$this->errorLevel = -1;
* 1 if entered password matches with one in db
* 0 if it doesn match
* -2 if could not nonnect to db;
private function is_password_correct()
if($this->errorLevel == -2)//this should never happen
$row = mysql_fetch_assoc($this->queryResults);// puts query results in array
$salt = substr ($row['password'], 0, strlen($row['password'])/2); //takes salt from query result
$hash = hash("sha256", $salt . $this->password);
$saltAndPassword = $salt . $hash;
if($saltAndPassword == $row['password'])
Template Name: login
// Inialize session
// Include database connection settings
//Include login class
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$login = new login();
and this is config.php where i connect to DB
$hostname = 'localhost'; // Your MySQL hostname. Usualy named as 'localhost', so you're NOT necessary to change this even this script has already online on the internet.
$dbname = 'I am'; // Your database name.
$username = 'not that'; // Your database username.
$password = 'stupid :D '; // Your database password. If your database has no password, leave it empty.
// Let's connect to host
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
// Select the database
mysql_select_db($dbname) or DIE('Database name is not available!');
EDIT: I forgot to write what have i done to search for problem.
I checked and saw that my code failed to log in and doesnt provide an error.
Then i checked if with correct user name and password it would set session variable. No it didint.
Then i find out that it also didint change $errorLevel.
Then i checked all variables and find out that $quearyResults is empty, but mysql_error() was also empty. So it connects to db but query is empty