-1

Someone please help, I have been struggling with this for hours. I am trying to retrieve all rows but keep getting this error. It is returning bool(true) but wont go any further than that. My code is posted underneath:

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);
  }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Emma
  • 19
  • 3
  • 1
    Read the PHP documentation and look at the example http://php.net/manual/en/pdostatement.execute.php http://php.net/manual/en/pdostatement.fetchall.php – Charlotte Dunois Aug 22 '16 at 10:09

1 Answers1

0

As you already have said, the execute method returns true (so a boolean). Now you try to call the method fetchAll on this boolean which of course does not work.

As Charlotte Dunois has already suggested in the comment section, you should take a look at the php manual. There you can see, that you must call the fetchAll method as a member of the statement.

So the Event.load.php should look something like this:

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";
}
Andreas
  • 309
  • 1
  • 8