2016-08-22 11 views
-1

有人請幫忙,我一直在掙扎幾個小時。我試圖檢索所有行,但不斷收到此錯誤。它正在返回布爾(真),但不會比這更進一步。我的代碼貼下面:PHP致命錯誤:調用一個非對象的成員函數fetchAll() - PDO語句返回TRUE

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

閱讀PHP文件,並看一下例子http://php.net/manual/en/pdostatement.execute.php HTTP ://php.net/manual/en/pdostatement.fetchall.php –

回答

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"; 
} 
相關問題