Ошибка при использовании prepare в mysqli
Привет, есть ли кто-нибудь, кто может мне помочь ?
мне очень трудно решить эту ошибку:
Fatal error: Uncaught Error: Call to undefined method Connection::prepare()
Вот мой код :
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); }
Кстати, я новичок в классе php, заранее благодарю вас за помощь!
Что я уже пробовал:
Я перепробовал все, что мог придумать, я пытаюсь сделать это :
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; }
но не повезло!