PHP致命错误:在非对象上调用成员函数fetchAll() - PDO语句返回TRUE

时间:2016-08-22 10:06:28

标签: php mysql json pdo

有人请求帮助,我一直在努力工作几个小时。我试图检索所有行但仍然收到此错误。它回归bool(真实),但不会再进一步​​了。我的代码发布在下面:

Event.load.php

include $_SERVER['DOCUMENT_ROOT'] . '/includes/database.php';

try 
{
    $db = new Database();
    $user = new USER( $db );
    $stmt = $user->runQuery( 'SELECT id, name, DATE_FORMAT( start_date, "%D %b %Y" ) start_date, DATE_FORMAT( start_date, "%l:%i %p" ) start_time, DATE_FORMAT( end_date, "%D %b %Y" ) end_date, DATE_FORMAT( end_date, "%l:%i %p" ) end_time, description FROM event');
    $rslt= $stmt->execute();
    $result = $rslt->fetchAll();
// header("Content-Type: application/json", true);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

database.php中

class Database
{
    public function dbConnection()
    {
        $this->conn = null;    
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }
        return $this->conn;
    }
}

include_once 'user.php';
$db = new Database();
$user = new USER($db);
?>

user.php的

<?php

require_once 'database.php';

class USER
{ 

 private $conn;

 public function __construct()
 {
  $database = new Database();
  $db = $database->dbConnection();
  $this->conn = $db;
    }

 public function runQuery($sql)
 {
  $stmt = $this->conn->prepare($sql);
  return $stmt;
 }

 public function lasdID()
 {
  $stmt = $this->conn->lastInsertId();
  return $stmt;
 }

 public function register($uname,$email,$upass,$code)
 {
  try
  {       
   $password = md5($upass);
   $stmt = $this->conn->prepare("INSERT INTO users(userName,userEmail,userPass,tokenCode) 
                                                VALUES(:user_name, :user_mail, :user_pass, :active_code)");
   $stmt->bindparam(":user_name",$uname);
   $stmt->bindparam(":user_mail",$email);
   $stmt->bindparam(":user_pass",$password);
   $stmt->bindparam(":active_code",$code);
   $stmt->execute(); 
   return $stmt;
  }
  catch(PDOException $ex)
  {
   echo $ex->getMessage();
  }
 }

 public function login($email,$upass)
 {
     try
     {
         $stmt = $this->conn->prepare("SELECT * FROM users WHERE userEmail=:email_id");
         $stmt->execute(array(":email_id"=>$email));
         $userRow=$stmt->fetch(PDO::FETCH_ASSOC);

       if($stmt->rowCount() == 1)
       {
           if($userRow['userStatus']=="Y")
       {
           if($userRow['userPass']==md5($upass))
           {
               $_SESSION['userSession'] = $userRow['userID'];
               return true;
           }
           else
           {
               header("Location: index.php?error");
               exit;
           }
       }
       else
       {
           header("Location: index.php?inactive");
           exit;
       } 
   }
   else
   {
       header("Location: index.php?error");
       exit;
   }  
  }
  catch(PDOException $ex)
  {
   echo $ex->getMessage();
  }
 }


 public function is_logged_in()
 {
  if(isset($_SESSION['userSession']))
  {
   return true;
  }
 }

 public function redirect($url)
 {
  header("Location: $url");
 }

 public function logout()
 {
session_start();
  session_destroy();
  $_SESSION['userSession'] = false;
 }

 function send_mail($email,$message,$subject)
 {   
    mail($email, $subject, $message);
  }
}

1 个答案:

答案 0 :(得分:0)

正如您已经说过的,execute方法返回true(所以布尔值)。 现在,您尝试在此布尔值上调用方法fetchAll,这当然不起作用。

正如Charlotte Dunois已经在评论部分提出的那样,你应该看一下php手册。在那里你可以看到,你必须将fetchAll方法作为语句的成员来调用。

所以Event.load.php应该是这样的:

try 
{
    $db = new Database();
    $user = new USER( $db );
    $stmt = $user->runQuery( '...');
    $rslt= $stmt->execute();
    $result = $stmt->fetchAll(); //<----THIS LINE IS IMPORTANT
// header("Content-Type: application/json", true);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}