Erreur Trying to get property of non-object

Par FrenchGaming Team, il y a 10 ans


Bonsoir,
Voila j’obtiens l'erreur suivante : Voir screen :

Pourtant j’obtiens le résultat de la fonction voir le screen ci-dessus et code ci dessous

Dans ma fonction getActivate

PHP: public function getActivate($db, $key) { $tokens = $db->query("SELECT * FROM security WHERE token = ? LIMIT 1", [$key])->fetch(); if($tokens->token === $key ) { return true; } else { return false; }

Dans le register.php

$db = App::getDatabase(); $validator = new Validator($_POST); if(empty($_POST['submit'])) { if(App::getAuth()->getActivate($db, $_POST['key'])) { echo "ok"; } else { echo "non"; } }

Pour info voici ce que je déclare a PDO

PHP:
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);

8 réponses

tleb, il y a 10 ans

Salut,

Il faudrait que tu nous dises quel ligne c'est la ligne 43.

J'imagine que c'est if($tokens->token === $key ). Si c'est ça, c'est que tu as un problème avec ta requête.

FrenchGaming Team, il y a 10 ans

Oui c'est sa

Alexandre #lbac, il y a 10 ans

Bonjour,

En faisant un var_dump( sur $tokens ça donne quoi ?

Kraddle, il y a 10 ans

salutations
et avec

if($tokens[0]->token === $key )
FrenchGaming Team, il y a 10 ans

Squallx
Voici le code du var_dump()

object(stdClass)[8] public 'id' => string '1' (length=1) public 'token' => string 'AgiMMJXZVzxIemYOrRajaiH8G6KoK1scKHqaZ7inSs9LvlDNdz4p2gszt2Hf' (length=60) public 'confirmed' => null
FrenchGaming Team, il y a 10 ans

Kradlle voici l'erreur que j'ai en faisant ton code

( ! ) Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\api\lib\Auth.php on line 43
FrenchGaming Team, il y a 10 ans

Erreur résolue voici le code qui me fallait

public function getActivate($db, $key) { $tokens = $db->query("SELECT * FROM security WHERE token = ? LIMIT 1", [$key])->fetch(); if($tokens && $tokens->token == $key) { return true; } return false; }
Kraddle, il y a 10 ans

et avec

$tokens = $db->query("SELECT * FROM security WHERE token = ? LIMIT 1", [$key])->fetch(PDO::FETCH_OBJ);

Ou

$tokens = $db->prepare("SELECT * FROM security WHERE token = ? LIMIT 1"); $tokens->excute(array($key)); $tokens->fetch(PDO::FETCH_OBJ);