Page 1 sur 1

Notice: Trying to access array offset on value of type bool

Posté : 28 sept. 2021, 10:41
par david7675
J'essaye de retourner la liste des ateliers dans un programme de séminaire en ligne cela marche tout s'affiche mais j'ai une erreur de type bool
Merci de votre aide


Notice: Trying to access array offset on value of type bool in /home/apache/wseminaire/www/model/ModelParticipant.php on line 51

Code : Tout sélectionner

<?php include_once('src/db/connect.php'); class ModelParticipant { private $connect; public function __construct() { // Récupération de la connexion à la bdd $cnt = new connect; $this->connect = $cnt->setConnection(); } public function read() { // Récupération des données événements $info = array(); $nbr = array(); $req2 = $this->connect->query('SELECT id, name_ev, date_ev FROM event ORDER BY date_ev DESC'); $res = $this->connect->query('SELECT id, name_ev, date_ev FROM event ORDER BY date_ev DESC'); while ($data2 = $req2->fetch()) { $id = $data2['id']; // Réucpèrer le nombre de participants $req3 = $this->connect->prepare('SELECT COUNT(id) AS nbr FROM participant WHERE id_event=:value'); $req3->bindParam(':value', $id, PDO::PARAM_INT); $req3->execute(); array_push($nbr, $req3->fetch()); $req3->closeCursor(); } $return = array("res" => $res, "info" => $info, "nbr" => $nbr); return $return; } public function readDetail($id) { $req = $this->connect->prepare('SELECT id, id_event, name, lastname, fonction, tel, mail, particip, dej, din, ateliers FROM participant WHERE id_event=:value'); $req->bindParam(':value', $id, PDO::PARAM_INT); $req->execute(); $req1 = $this->connect->prepare('SELECT ateliers FROM participant WHERE id_event=:value'); $req1->bindParam(':value', $id, PDO::PARAM_INT); $req1->execute(); $atelier = array(); $i = 0; while ($data = $req1->fetch()){ $at = explode(',', $data['ateliers']); $atelier[$i] = ""; foreach ($at as $key) { $req2 = $this->connect->prepare('SELECT name_atelier FROM atelier where id=:value'); $req2->bindParam(':value', $key, PDO::PARAM_INT); $req2->execute(); $nameAt = $req2->fetch(); $atelier[$i] .= $nameAt['name_atelier']."<br />"; } $i++; } $res = $req; return array("1" => $res, "2" => $atelier); }

Re: Notice: Trying to access array offset on value of type bool

Posté : 28 sept. 2021, 14:28
par @rthur
Quelle est la ligne 51 ?
Ce message d'erreur indique que tu essayes de traiter une variable comme un tableau, or cette variable est de type booléen (true/false)
Fais un var_dump() des variables de la lignes 51 et remonte ensuite dans ton code à quel endroit est défini cette variable.

D'expérience, c'est souvent une requête SQL qui échoue et donc qui retourne false au lieu de renvoyer une liste de résultats

Re: Notice: Trying to access array offset on value of type bool

Posté : 28 sept. 2021, 14:42
par david7576
C'est cette ligne

$atelier[$i] .= $nameAt['name_atelier']."<br />";

Je ne trouve pas l'erreur pourrais-tu m'aider dans le code ?

Re: Notice: Trying to access array offset on value of type bool

Posté : 28 sept. 2021, 15:21
par david7576
J'ai fait un var_dump j'ai bien des valeurs qui s'affichent !

Re: Notice: Trying to access array offset on value of type bool

Posté : 28 sept. 2021, 16:07
par @rthur
var_dump($atelier, $nameAt);
Tu as forcément un true ou un false, si tu as ce message d'erreur et si ces 2 variables sont à la ligne 51

Re: Notice: Trying to access array offset on value of type bool

Posté : 28 sept. 2021, 16:18
par david7576
J'ai cela qui s'affiche
" } bool(false) array(2) { [0]=> string(33) "Visio 1 : 08h30-10h00

Re: Notice: Trying to access array offset on value of type bool

Posté : 29 sept. 2021, 09:43
par david7576
J'ai tout essayé je ne vois pas

Re: Notice: Trying to access array offset on value of type bool

Posté : 29 sept. 2021, 10:16
par @rthur
C'est étrange que var_dump($atelier) soit à false alors que dans le code que tu indiques il devrait être un tableau vide...


As-tu activé la gestion d'erreur de PDO sur PDO::ERRMODE_WARNING ?
https://www.php.net/manual/fr/pdo.error-handling.php
Cela peut aider à débuguer.

Re: Notice: Trying to access array offset on value of type bool

Posté : 29 sept. 2021, 16:25
par david7576
oui c'est activé
pouvez-vous m'aider ?

Re: Notice: Trying to access array offset on value of type bool

Posté : 30 sept. 2021, 10:00
par david7576
J'ai trouvé
$atelier[$i] .= $nameAt['name_atelier'] ?? '' ."<br />";