Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello is there anyone can help me ?
i'm having a hard time trying to solve this error:

Fatal error: Uncaught Error: Call to undefined method Connection::prepare()


Here's my code :

class Connection
	{

		function __construct($hostname,$username,$password,$database)
		{
			# code...
			$this->hostname = $hostname;
			$this->username = $username;
			$this->password = $password;
			$this->database = $database;
			
		}

		function setConnection(){
			$this->connection = new mysqli($this->hostname,$this->username,$this->password,$this->database);
			if(!$this->connection) die('The application is not available at the moment. Please try again later.');
		}
	}

//heres my code for login:
class Login
	{
		
		function __construct(Connection $connection)
		{
			# code...
			$this->connection = $connection;
		}

		function setLogin($username, $password){
			$this->username = $username;
			$this->password = $password;

			$login_query = $this->connection->prepare('SELECT username, password FROM admin WHERE username =? AND password =?');
			$login_query->bind_param('ss', $this->username, $this->password);
			$login_query->execute();

			$checkIfExist = $login_query->fetch();
			if($checkIfExist['name'] == $this->username && $checkIfExist['password'] == $this->password){
				header('location: hello.php');
			}

		}
	}

//and heres my code in my main index.php:
        include_once 'database.connection.php';
	include_once 'database.login.php';

	$dbconnect = new Connection('localhost','root','','inventory');
	$dbconnect->setConnection();

	$dblogin = new Login($dbconnect);

	if(isset($_POST['username']) && isset($_POST['password'])){
		$username = htmlentities(mysqli_escape_string($dbconnect->connection, $_POST['username']));
		$password = htmlentities(mysqli_escape_string($dbconnect->connection, $_POST['password']));

		$dblogin->setLogin($username, $password);
	}


Im new to class in php by the way, Thank you in advance for helping me!

What I have tried:

I Tried everything i could think of, i try to do this :
function setConnection(){
			$this->connection = new mysqli($this->hostname,$this->username,$this->password,$this->database);
			if(!$this->connection) die('The application is not available at the moment. Please try again later.');

            //i tried to return my connection using this
			return $this->connection;
		}



but no luck!
Posted
Updated 10-Jan-20 22:47pm

1 solution

You are passing a Connection object into your Login class. But the mysqli object is the connection variable inside the Connection class. So your Login constructor should actually be:
PHP
function __construct(Connection $connection)
{
    # code...
    $this->connection = $connection->connection;
}

I think your use of the term connection as the class name and the connection name is confusing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900