完全なスクリプトは次のとおりです。
connection.php
class Connection{
public function dbConnect(){
return new PDO("mysql:host=localhost; dbname=test", "root", "");
}
}
user.php
include_once('connection.php');
class User{
private $db;
public function __constructor(){
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function Login($name, $pass){
if(!empty($name) && !empty($pass)){
$st = $this->db->prepare("SELECT * FROM users WHERE name =? and pass =?");
$st->bindParam(1, $name);
$st->bindParam(2, $pass);
$st->execute();
if($st->rowCount() == 1){
echo "User verified. Access granted";
}else{
echo "Incorrect";
}
}else{
echo "Please enter name and password";
}
}
}
index.php
include_once('User.php');
if(isset($_POST['submit'])){
$name = $_POST['user'];
$pass = $_POST['pass'];
$object = new User();
$object->Login($name, $pass);
}
しかし、試してみると、次のエラーが表示されます。
致命的なエラー:16行目のC:\ wamp \ www \ tests \ User.phpの非オブジェクトでメンバー関数prepare()を呼び出す
16行目:$ st = $ this-> db-> prepare( "SELECT * FROM users WHERE name =?and pass =?");