MySQL Errors

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

Post by Aleeious »

I am having a small problem. In my register.php page i use 2 classes to register my user, Database.class.php and User.class.php. In register.php i check to see if the username exists. However when i try to run exist finction i get the error:
Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/aleeious/public_html/beta/libs/User.class.php on line 78
Here is the code i'm using
register.php

Code: Select all

<?php

// include smarty library
require('libs/Smarty.class.php');

// include database class
require_once('libs/Database.class.php');

// include user class
require_once('libs/User.class.php');

// create instance of smarty library
$smarty = new Smarty();

// if the form wasn't submited
if(!isset($_POST["submit"]))
{
	// display it
	$smarty->display('register.tpl');
}
// otherwise the form was submitted
else
{
	// if the username is empty
	if(empty($_POST["username"]))
	{
		// so display an error stating the username is empty
		$smarty->assign('error', 'username is empty');

		// and display it
		$smarty->display('error.tpl');

		// and terminate
		exit;
	}
	// if the username is too short or too big
	elseif(strlen($_POST["username"]) < 4 || strlen($_POST["username"]) > 16)
	{
		// so display an error stating the required usernamelength
		$smarty->assign('error', 'username must be 4-16 characters long');

		// and display it
		$smarty->display('error.tpl');

		// and terminate
		exit;
	}
	// otherwise the username is filled in
	else
	{
		// so sanitize it
		$username = $_POST["username"];
	}

	// if the password is empty
	if(empty($_POST["password"]))
	{
		// so display an error stating the password is empty
		$smarty->assign('error', 'password is empty');

		// and display it
		$smarty->display('error.tpl');

		// and terminate
		exit;
	}
	// if the password is too short or too big
	elseif(strlen($_POST["password"]) < 4 || strlen($_POST["password"]) > 16)
	{
		// so display an error stating the required password length
		$smarty->assign('error', 'password must be 4-16 characters long');

		// and display it
		$smarty->display('error.tpl');

		// and terminate
		exit;
	}
	// otherwise the password is ok
	else
	{
		// so sanitize it
		$password = $_POST["password"];
	}

	// if the password verification is empty
	if(empty($_POST["verifypassword"]))
	{
		// so display an error stating the password verification is empty
		$smarty->assign('error', 'you must verify your password');

		// and display it
		$smarty->display('error.tpl');

		// and terminate
		exit;
	}
	// if the password and password verification don't match
	elseif($_POST["password"] != $_POST["verifypassword"])
	{
		// so display an error stating the passwords don't match
		$smarty->assign('error', 'both password and verify password must match');

		// and display it
		$smarty->display('error.tpl');

		// and terminate
		exit;
	}
	// otherwise the password verification is ok
	else
	{
		// so sanitize it
		$verifypassword = $_POST["verifypassword"];
	}

	// if the email is empty
	if(empty($_POST["email"]))
	{
		// so display an error stating the email is empty
		$smarty->assign('error', 'email is empty');

		// and display it
		$smarty->display('error.tpl');

		// and terminate
		exit;
	}
	// if the email is invalid
	//elseif(strlen($_POST["email"]) < 4 || strlen($_POST["password"]) > 16)
	//{
		// so display an error stating the email is invalid
		//$smarty->assign('error', 'the mail address you entered is invalid');

		// and display it
		//$smarty->display('error.tpl');

		// and terminate
		//exit;
	//}
	// otherwise the email is ok
	else
	{
		// so sanitize it
		$email = $_POST["email"];
	}

	// open a connection to the database
	$db = new Database();

	// create an instance of the user class
	$user = new User($db);

	// check to see if the username already excists
	if($user->exists($username))
	{
		// if the username already excists display an error stating it
		$smarty->assign('error', 'that username already exists');

		// display it
		$smarty->display('error.tpl');	
	}

	// if everything checks out display an error stating registrations are currently closed
	$smarty->assign('error', 'registrations are currently disabled');

	// display it
	$smarty->display('error.tpl');
}

?>
Database.class.php

Code: Select all

 <?php

// import configuration data
require_once('./config.php');

class Database extends mysqli
{
	// instance of class
	private $instance;

	// class constructor
	public function __construct()
	{
		// turn on error reporting
        mysqli_report(MYSQLI_REPORT_ALL);

		parent::__construct(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);

		// if the connection couldn't be established
		if(mysqli_connect_errno())
		{
			// throw an exception
			throw new exception(mysqli_connect_error(), mysqli_connect_errno()); 
		}
	}

	// returns instance of class
	public static function getInstance() {

		// if an instance doesn't excist
		if(!$this->instance)
		{
			// create a new instance
			$this->instance = new self(); 
		}

		// otherwise return the class instance
		return $this->instance;
	}
}

?>
User.class.php

Code: Select all

<?php

class User
{
	// database instanceof
	protected $db;

	// username
	private $username;

	// user's password
	private $password;

	// user's email
	private $email;

	/**
	* class constructor
	*/
	public function __construct(Database $db)
	{
		// get instance of database
		$this->db = $db;
	}

	/**
	* retruns username
	*/
	public function getUsername()
	{
		return $this->password;
	}

	/**
	* retruns password
	*/
	public function getPassword()
	{
		return $this->password;
	}

	/**
	* retruns email
	*/
	public function getEmail()
	{
		return $this->email;
	}

	/**
	* adds the user to the database
	* @param username of user to add
	* @param password of user to add
	* @param email address of user to add
	*/
	public function add($username, $password, $email)
	{
		
	}

	/**
	* checks if the user already excists in the database
	* @param username of user to check
	* @returns true if username excists, otherwise false
	*/
	public function exists($username)
	{
		// prepare the sql statement
		$statement = $this->db->prepare('SELECT id FROM ' . TABLE_USERS . ' WHERE USERNAME = ?');

		// bing the variables
		$statement->bind_param('i', $id);

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

			// if a result exists
			if($result)
			{
				// return true
				return true;
			}
			
			// otherwise return false
			return false;
        }
	}

	/**
	* gets user info and populates it
	* @param username of user to retrieve info of
	*/
	public function getInfo($username)
	{
		
	}

}

?>
Line 78 in /home/aleeious/public_html/beta/libs/User.class.php is:

Code: Select all

$result = $statement->get_result();
Any assistance in this matter would be greatly appreciated.

Sincerely,

Aleeious
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: MySQL Errors

Post by Jackolantern »

I am no sure if this is causing the issue, but one thing that definitely looks like an error:

Code: Select all

 /**
   * retruns username
   */
   public function getUsername()
   {
      return $this->password;
   }

   /**
   * retruns password
   */
   public function getPassword()
   {
      return $this->password;
   } 
You are returning $password for both the getUsername and getPassword methods. If you are looking for a user with a username of their password, you would be coming up empty handed, which may be propagating an error through your scripts.
The indelible lord of tl;dr
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: MySQL Errors

Post by MikuzA »

I'm not a pro on the classes but this looks weird in the Database.class.php, if it's not the issue can you/someone explain why:

Code: Select all

public function __construct(Database $db) 
What is the 'Database' in the construct?

Shouldn't it be just,

Code: Select all

public function __construct($db) 
As said, I'm no expert on this matter but just a thought :)
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: MySQL Errors

Post by MikuzA »

OK, google answered why it's like that..

but,

in Users.class you have:

Code: Select all

   // open a connection to the database
   $db = new Database();

   // create an instance of the user class
   $user = new User($db); 
Wouldn't that be like double calling of Database-class?
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: MySQL Errors

Post by MikuzA »

Oh ignore everything I wrote before :) I took some time on your interesting problem.

In the User.class

Code: Select all

$statement->bind_param('i', $id); 
By understanding the bind_param, it swaps the '?' from the prepared query.
But you are comparing username towards an integer, is that correct?

I still might be way off on what's wrong but It's interesting!
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: MySQL Errors

Post by Winawer »

mysqli_stmt only has a get_result method if you're using mysqlnd. Use bind_result and fetch instead.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: MySQL Errors

Post by Jackolantern »

MikuzA wrote:I'm not a pro on the classes but this looks weird in the Database.class.php, if it's not the issue can you/someone explain why:

Code: Select all

public function __construct(Database $db)
What is the 'Database' in the construct?

Shouldn't it be just,

Code: Select all

public function __construct($db)
As said, I'm no expert on this matter but just a thought :)
That is PHP "type hinting". It is kind of an odd feature of PHP, where you can suddenly make your function arguments strongly typed, basically. What it is saying is that the constructor will throw an error if it received anything except a Database resource object.
The indelible lord of tl;dr
Aleeious
Posts: 55
Joined: Tue Sep 13, 2011 1:22 pm

Re: MySQL Errors

Post by Aleeious »

This doesn't produce any errors, but it will always state they username is not in use, even when it is. Here is the code I'm using:

Code: Select all

	/**
	* checks if the user already excists in the database
	* @param username of user to check
	* @returns true if username excists, otherwise false
	*/
	public function exists($username)
	{
		// prepare the sql statement
		$statement = $this->db->prepare('SELECT id FROM ' . TABLE_USERS . ' WHERE USERNAME = ? LIMIT 1');

		// bing the variables
		$statement->bind_param('i', $id);

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

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

			// if a result exists
			if($row)
			{
				// return true
				return true;
			}
			
			// otherwise return false
			return false;
        }
	}
Any assistance would be greatly appreciated.

Sincerely,

Aleeious
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: MySQL Errors

Post by MikuzA »

MikuzA wrote:Oh ignore everything I wrote before :) I took some time on your interesting problem.

In the User.class

Code: Select all

$statement->bind_param('i', $id);
By understanding the bind_param, it swaps the '?' from the prepared query.
But you are comparing username towards an integer, is that correct?

I still might be way off on what's wrong but It's interesting!
But the username you are comparing is an STRING right? You are binding integer and $id to the parameter..
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: MySQL Errors

Post by Winawer »

Yeah, looks like you need $statement->bind_param('s', $username); in there.
Post Reply

Return to “Beginner Help and Support”