Page 1 of 1

MySQL Errors UGH!

Posted: Tue Oct 15, 2013 11:11 pm
by Aleeious
Hey guys having a new set of probems now related to changing a forgotten users password. When i run it i get the error:
PASSWORD = azo0rI2YDd
PASSWORD HASH = $2a$12$sG77iQzsLR1aRhw8cvNpNOHRDkwAChuWYY694UpkQ/J6ZVPKp2B1m
STATEMENT =

Fatal error: Call to a member function bind_param() on a non-object in /home/aleeious/public_html/beta/libs/User.class.php on line 303
Anyways heres my code:

Code: Select all

/**
	* Generates a hash
	* @param $password password to hash
	* @return hashed password
	*/
	public function generateHash($password, $salt, $rounds = 12)
	{
		// return the password hash using crypt
		return crypt($password, sprintf('$2a$%02d$', $rounds) . $salt);
	}

	/**
	* Generates a random password
	* @return generated random password
	*/
	public function generateRandomPassword($length)
	{
		// list the characters valid in the salt
		$validcharacters = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9'), range('!', ')'));
		
		// repeat the loop 22 times cause the salt is 22 characters
		for($i = 0; $i < $length; $i++)
		{
			// generate the salt
			$password .= $validcharacters[array_rand($validcharacters)];
		}

		// return the new password
		return $password;
	}

/**
	* changes the users password 
	* @param $username username to check
	* @param $email user email tied to user
	* @return true if login is successful otherwise false
	*/
	public function resetPassword($username, $email)
	{
		// prepare the sql statement
		$statement = $this->db->prepare('SELECT salt FROM ' . TABLE_USERS . ' WHERE USERNAME = ? AND EMAIL = ? LIMIT 1');

		// bing the variables
		$statement->bind_param('ss', $username, $email);

		// if the statement executed successfully
		if ($statement->execute())
		{
			// get the number of results
			$statement->bind_result($resultsalt);

			// fetch the results
			$statement->fetch();

			// if a result exists
			if($resultsalt)
			{
				// generate a new password
				$password = $this->generateRandomPassword(10);

				// DEBUG
				echo "PASSWORD = $password<br />";

                // hash the password using the stored hash
                $passwordhash = $this->generateHash($password, $resultsalt);

				// DEBUG
				echo "PASSWORD HASH = $passwordhash<br />";

				// prepare the sql statement to change the password
				$statement = $this->db->prepare('UPDATE ' . TABLE_USER . ' SET PASSWORD = ? WHERE USERNAME = ? AND EMAIL = ? LIMIT 1');

				echo "STATEMENT = $statement<BR />";

				// bing the variables
				$statement->bind_param('sss', $passwordhash, $username, $email);

				// if the statement executed successfully
				if ($statement->execute())
				{
					// get the number of results
					$statement->bind_result($result);

					// fetch the results
					$statement->fetch();

					// if a result exists
					if($result)
					{
						echo "New Password: $password<br />";
						
						// return success
						return true;
					}
        		}
				else
				{
					// otherwise the query couldn't be run so return false
					return false;
				}
			}
		}
		else
		{
			// otherwise the query couldn't be run so return false
			return false;
		}
	}
Any assistance in this matter would be greatly appreciated.

Sincerely,

Aleeious

Re: MySQL Errors UGH!

Posted: Tue Oct 15, 2013 11:19 pm
by Epiales
Not saying I can figure anything out, but I have been working some with mysqli and binding recently. And I learn this way too :) Is this your User.class.php on line 303 page? I only see 110 lines??

Re: MySQL Errors UGH!

Posted: Tue Oct 15, 2013 11:52 pm
by Aleeious
Epiales wrote:Not saying I can figure anything out, but I have been working some with mysqli and binding recently. And I learn this way too :) Is this your User.class.php on line 303 page? I only see 110 lines??
This is just the parts related to my error of User.class.php and line 303 translates to:

Code: Select all

// prepare the sql statement to change the password
$statement = $this->db->prepare('UPDATE ' . TABLE_USER . ' SET PASSWORD = ? WHERE USERNAME = ? AND EMAIL = ? LIMIT 1');
since i removed all the unrelated code. If you want, i will post the full class code. Any assistance would be greatly appreciated.

Sincerely,

Aleeious

Re: MySQL Errors UGH!

Posted: Wed Oct 16, 2013 12:45 am
by Epiales
Aleeious wrote:
Epiales wrote:Not saying I can figure anything out, but I have been working some with mysqli and binding recently. And I learn this way too :) Is this your User.class.php on line 303 page? I only see 110 lines??
This is just the parts related to my error of User.class.php and line 303 translates to:

Code: Select all

// prepare the sql statement to change the password
$statement = $this->db->prepare('UPDATE ' . TABLE_USER . ' SET PASSWORD = ? WHERE USERNAME = ? AND EMAIL = ? LIMIT 1');
since i removed all the unrelated code. If you want, i will post the full class code. Any assistance would be greatly appreciated.

Sincerely,

Aleeious
Try to debug it and see if it gives you any information. Put after the prepare statement:

var_dump($statement );

Re: MySQL Errors UGH!

Posted: Wed Oct 16, 2013 1:16 am
by Epiales

Code: Select all

            $statement->bind_param('sss', $passwordhash, $username, $email);
You sure it's not iss instead of sss?

Re: MySQL Errors UGH!

Posted: Wed Oct 16, 2013 3:24 am
by Aleeious
Epiales wrote:

Code: Select all

            $statement->bind_param('sss', $passwordhash, $username, $email);
You sure it's not iss instead of sss?
Yes i'm sure:
Aleeious wrote:Hey guys having a new set of probems now related to changing a forgotten users password. When i run it i get the error:
PASSWORD = azo0rI2YDd
PASSWORD HASH = $2a$12$sG77iQzsLR1aRhw8cvNpNOHRDkwAChuWYY694UpkQ/J6ZVPKp2B1m
STATEMENT =

Fatal error: Call to a member function bind_param() on a non-object in /home/aleeious/public_html/beta/libs/User.class.php on line 303
Any assistance in this matter would be greatly appreciated.

Sincerely,

Aleeious

Re: MySQL Errors UGH!

Posted: Wed Oct 16, 2013 5:23 am
by Winawer
I think TABLE_USER is supposed to be TABLE_USERS.

Re: MySQL Errors UGH!

Posted: Wed Oct 16, 2013 4:41 pm
by Epiales
Winawer wrote:I think TABLE_USER is supposed to be TABLE_USERS.
Good catch :)