0

如果配置类,我首先有三个文件,第二个是用户类,第三个是索引文件。

配置文件

class connection{    

    public function dbConnect()
    {         
       new PDO("mysql:host=localhost:3306;dbname=education","root","");
    }
}

用户.php

<?php
session_start();
require_once '../cms/config.php';

class user{
    private $db;

    public function __construct()
    {
        $this->db= new connection();
        $this->db=  $this->db->dbConnect();
        return $this->db;
    }

    public function login($user,$pass)
    {
        //creating a array for future usage

        if(!empty($user) && !empty($pass))
        {                
           $st=$this->db->prepare("select * from admin where admin_user=? AND admin_pass=?");
           $st->bindParam(1,$user);
           $st->bindParam(2,$pass);
           $st->execute();
           if($st->rowCount()==1)
           {
             $_SESSION['admin_user']=$user;
             header("Location:admin.php");
           }
           else
           {
               $_SESSION['errorMessage']="Incorrect username or password!Please try again.";
               echo $_SESSION['errorMessage'];
           }               
        }
        else
        {
             echo "Hey! Don't leave me empty";                
        }
    }
}
?>

索引.php

require_once '../cms/user.php';

if(isset($_POST['submit']))
{    
    $user=$_POST['user'];
    $user=  mysql_real_escape_string($user);
    $pass=$_POST['pass'];
    $pass=  mysql_real_escape_string($pass);

    $object=new user();

    $object->login($user,$pass);
}

当我尝试使用错误或正确的数据登录时,它会引发致命错误。即使它是昨天的工作文件。但是今天发生了什么。这很烦人。请帮忙。

4

1 回答 1

0

您需要返回 pdo 实例。

class connection{    

    public function dbConnect()
    {    
         // here, you need to return it     
         return new PDO("mysql:host=localhost:3306;dbname=education","root","");
    }
}
于 2012-10-20T05:55:58.013 回答