这是完整的脚本:
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 \ testes \ User.php中的非对象上调用成员函数prepare()
第16行:$ st = $ this-> db-> prepare(“SELECT * FROM users WHERE name =?and pass =?”);
答案 0 :(得分:2)
你的魔法被破坏了
public function __constructor(){
应该是
public function __construct(){