1

I get this error when I try to put the id on button

Fatal error: Cannot use object of type PDOStatement as array...

The code where is the error is this

   $pdo = Database::connect();
   $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

   $q = "SELECT * FROM books where user = '".$_SESSION['level']."'";

   if($res = $pdo->query($q))
   {
         if($res->fetchColumn() > 0) 
         {
                foreach($pdo->query($q) as $res)
                {
                        echo '<a href="users/books.php?user_id='. $res['user_id'] .'"> '.$res['name'].' </a>';         
                }
         }
         else
         {
                echo '<a href="users/bookAdd.php?user_id='. $res['user_id'] .'">Create Book</a>';
         }
    }   
    Database::disconnect();

What I trying is when user log if there is no books to show him button Create book. And the error is there in the else block where is users/bookAdd.php?user_id='. $res['user_id'] .' Any idea how to fix this?

Kevin
  • 41,694
  • 12
  • 53
  • 70
Goro
  • 499
  • 1
  • 13
  • 31
  • possible duplicate of [PHP PDO with foreach and fetch](http://stackoverflow.com/questions/15385965/php-pdo-with-foreach-and-fetch) – benomatis Dec 10 '14 at 13:11
  • you're using `$pdo->query($q)` directly in a `foreach`, that won't work... see the duplicate i posted above... – benomatis Dec 10 '14 at 13:12

1 Answers1

4

The error is already clear, you can't try to access indices in context of a PDOStatement object.

You can't just use ->query(), and then try to access the values from the created object. You haven't fetch the results yet.

You need to fetch the results properly:

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// prep the statement
$q = $pdo->prepare("SELECT * FROM books WHERE user = :level");
$q->bindParam(':level', $_SESSION['level']); // bind it
$q->execute();
// fetch the results
$results = $q->fetchAll(PDO::FETCH_ASSOC);
if(count($results) > 0) {
    foreach($results as $res) {
        echo '<a href="users/books.php?user_id='. $res['user_id'] .'"> '.$res['name'].' </a>';
    }
} else {
    echo '<a href="users/bookAdd.php">Create Book</a>';
}

Database::disconnect();
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • but I want to pass also the user_id on bookAdd.php – Goro Dec 10 '14 at 13:19
  • @Goro you can't pass any Id on it since there are no results found – Kevin Dec 10 '14 at 13:19
  • @Goro what is `$_SESSION['user']` anyways? is this the user id? – Kevin Dec 10 '14 at 13:21
  • $_SESSION['level'] not user .. and is type of user like `admin is 1` superadmin is 2 etc. – Goro Dec 10 '14 at 13:22
  • @Goro oh i see, then you must have `$_SESSION['user_id']` in your session somewhere? since this particular user is logged in right? you can use that inside the else – Kevin Dec 10 '14 at 13:23
  • Yes, on login form I have `$_SESSION['level'] = $res['level'];` – Goro Dec 10 '14 at 13:25
  • @Goro you should also add the `$_SESSION['user_id'] = $res['user_id']` in login, so that you could still reuse it even when the select on books yields empty. then, add `echo 'Create Book';` – Kevin Dec 10 '14 at 13:28
  • My mistake.. I've misspelled user_id. It's ok now. Thank you – Goro Dec 10 '14 at 13:42