MySQL Errors UGH!

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Aleeious
Posts: 55
Joined: Tue Sep 13, 2011 1:22 pm

MySQL Errors UGH!

Post 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
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: MySQL Errors UGH!

Post 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??
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Aleeious
Posts: 55
Joined: Tue Sep 13, 2011 1:22 pm

Re: MySQL Errors UGH!

Post 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
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: MySQL Errors UGH!

Post 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 );
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: MySQL Errors UGH!

Post by Epiales »

Code: Select all

            $statement->bind_param('sss', $passwordhash, $username, $email);
You sure it's not iss instead of sss?
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Aleeious
Posts: 55
Joined: Tue Sep 13, 2011 1:22 pm

Re: MySQL Errors UGH!

Post 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
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: MySQL Errors UGH!

Post by Winawer »

I think TABLE_USER is supposed to be TABLE_USERS.
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: MySQL Errors UGH!

Post by Epiales »

Winawer wrote:I think TABLE_USER is supposed to be TABLE_USERS.
Good catch :)
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”