2

I've decided to migrate my project to prepared statements, and I am with an error after executing the following code:

$sql = 'SELECT clanID FROM clan_users WHERE userID = :uid LIMIT 1';
$data = $this->pdo->prepare($sql);
$data->execute(array(':uid' => $uid));
$data->fetchAll();

return $data['0']['clanid'];

Error returned:

 Fatal error: Cannot use object of type PDOStatement as array in
/var/www/game/classes/Clan.class on line 689

var_dump($data) returns:

object(PDOStatement)[122]
    public 'queryString' => string 'SELECT clanID FROM clan_users WHERE userID = :uid LIMIT 1' (length=57)

The value of $uid is correct, and selecting manually at mysql returns the expected row. I also tried changing to

$data->fetch(PDO::FETCH_OBJ);

but didnt work too.

Any ideas? Thanks in advance.

Renato Massaro
  • 544
  • 1
  • 8
  • 18

1 Answers1

6

The $data variable is just the statement. You actually need the result of the $data->fetchAll() to be populated into a different variable like this

$data_array = $data->fetchAll();

You then work with $data_array, not with $data directly.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103