这是完整的脚本:
连接.php
class Connection{
public function dbConnect(){
return new PDO("mysql:host=localhost; dbname=test", "root", "");
}
}
用户.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";
}
}
}
索引.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 =?");