2012-10-20 54 views
0

我有三个文件,如果第一个配置类,第二个是用户类,第三个是索引文件。致命错误:调用一个非对象的成员函数prepare()在

的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错误。即使这是昨天的工作文件。但今天发生了什么。这很烦人。 请帮忙。

+0

感谢Corbin进行编辑。我试图纠正,但失败了。 – geekhere

+0

尝试将连接存储在变量中。 – Dev

回答

0

您需要返回pdo实例。

class connection{  

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

谢谢!它的工作。 – geekhere

相关问题