Erreur can not use stdclass object as array on line 49

Résolu/Fermé
LearnDeep Messages postés 67 Date d'inscription lundi 10 décembre 2018 Statut Membre Dernière intervention 21 août 2021 - Modifié le 1 oct. 2019 à 23:18
jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 - 2 oct. 2019 à 00:02
Bonjour , j'ai une erreur et je ne sais pas quoi faire pour résoudre ce problème.



<?php error_reporting(E_ALL); ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true ");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE,
OPTIONS'); header("Access-Control-Allow-Headers: X-Custom-Header,
Origin, Content-Type , Authorisation , X-Requested-With");
header("Content-Type: application/json; charset=UTF-8 ");
header("Referrer-Policy: origin-when-cross-origin");
header("Expect-CT: max-age=7776000, enforce");
header('Strict-Transport-Security: max-age=16070400;
includeSubDomains'); header('X-XSS-Protection: 1; mode=block');
header('X-Content-Type-Options: nosniff'); header('X-Frame-Options:
SAMEORIGIN');


$json = file_get_contents('php://input'); $decoded =
json_decode($json);

$id =    htmlspecialchars($decoded->id, ENT_QUOTES); $total =
htmlspecialchars($decoded->prix, ENT_QUOTES);

function conn() {   try{   $bdd =new
PDO('mysql:host=*****.mysql.db; dbname=*****;
charset=utf8', '*****', '*****');  
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
$bdd->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);   }
catch(PDOException $e) {
die('Erreur : ' . $e->getMessage()); }  return $bdd; }

if(empty($id) || empty($total) || !is_numeric($total)){
json_encode(false);
die; }else{

$db = conn();

$r = $db->prepare("SELECT id,adresse,tel FROM user WHERE email = ? ");
$r->execute([$id]); $v = $r->fetchAll(); $i = $v[0][0]; $d = $v[0][1];
$t = $v[0][2];

$prepred = $db->prepare("INSERT INTO commandes (etat, adresse, tel,
prix, idUser) VALUES('En Cours',?,?,?,?)"); $prepred->execute([$d, $t,
$total, $i]);

$x = $db->prepare("SELECT numCmd FROM commandes WHERE idUser = ? and
etat = 'En Cours'  "); $x->execute([$i]); $y = $x->fetchAll(); $r =
$y[sizeof($y)-1][0]; echo json_encode($r);

}


?>

1 réponse

jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 4 649
1 oct. 2019 à 23:19
A quoi correspond la ligne 49 ?
1
jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 4 649
Modifié le 2 oct. 2019 à 17:45
Ta ligne 49... ça ne serait pas celle la par hasard ?
$r = $y[sizeof($y)-1][0];

et donc.. il parait normal que tu aies un message indiquant que tu ne peux pas utiliser un objet en tant qu'array... vu que ta variable $y provient de ta requête....
$y = $x->fetchAll();

et que le mode de fetch que tu as choisi est le mode objet
$bdd->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);


Fais donc un var_dump de ta variable pour voir de quel type elle est et ce qu'elle contient...
0