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.');
}
}
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');
}
}
}
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.');
return $this->connection;
}
but no luck!