致命错误:在非对象中调用成员函数prepare()

时间:2012-10-20 05:42:12

标签: php pdo

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

的config.php

class connection{    

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

user.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";                
        }
    }
}
?>

的index.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);
}

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

1 个答案:

答案 0 :(得分:0)

您需要返回pdo实例。

class connection{    

    public function dbConnect()
    {    
         // here, you need to return it     
         return new PDO("mysql:host=localhost:3306;dbname=education","root","");
    }
}