Notice: Trying to access array offset on value of type bool in
Résolu
david76-21
Messages postés
14
Statut
Membre
-
jordane45 Messages postés 40050 Date d'inscription Statut Modérateur Dernière intervention -
jordane45 Messages postés 40050 Date d'inscription Statut Modérateur Dernière intervention -
Bonjour,
Avec la nouvelle version de php mon application de séminaire en ligne rencontre des erreurs.
Notice: Trying to access array offset on value of type bool in /home/apache/wseminaire/www/model/ModelAdmin.php on line 23
Notice: Trying to access array offset on value of type bool in /home/apache/wseminaire/www/model/ModelAdmin.php on line 29
Notice: Trying to access array offset on value of type bool in /home/apache/wseminaire/www/model/ModelAdmin.php on line 30
Pouvez-vous m'aider ?
Je vous en remercie par avance
Avec la nouvelle version de php mon application de séminaire en ligne rencontre des erreurs.
Notice: Trying to access array offset on value of type bool in /home/apache/wseminaire/www/model/ModelAdmin.php on line 23
Notice: Trying to access array offset on value of type bool in /home/apache/wseminaire/www/model/ModelAdmin.php on line 29
Notice: Trying to access array offset on value of type bool in /home/apache/wseminaire/www/model/ModelAdmin.php on line 30
<?php
include_once('src/db/connect.php');
class Admin {
private $connect;
public function __construct(){
// Récupération de la connexion à la bdd
$cnt = new connect;
$this->connect = $cnt->setConnection();
}
public function calendar(){
// Récupération des données événement pour le calendrier
$req = $this->connect->query('SELECT id, name_ev, date_ev FROM event ORDER BY ID');
return $req;
}
public function nextEv(){
// Récupération des données du prochain événement
$req = $this->connect->query('SELECT id, name_ev, date_ev FROM event WHERE date_ev > CURRENT_DATE ORDER BY date_ev ASC LIMIT 1');
$data = $req->fetch();
$id = $data['id'];
$req2 = $this->connect->prepare("SELECT COUNT(id) as nbr_participant FROM participant WHERE id_event = :id AND particip = 'oui'");
$req2->bindParam(':id', $id, PDO::PARAM_INT);
$req2->execute();
$data2 = $req2->fetch();
$particip = $data2['nbr_participant'];
$nextEv = $data['name_ev'];
$dateEv = $data['date_ev'];
$res = ['dateEv' => $dateEv, 'nextEv' => $nextEv, 'particip' => $particip];
return $res;
}
Pouvez-vous m'aider ?
Je vous en remercie par avance
A voir également:
- Notice: trying to access array offset on value of type bool
- Trying to access array offset on value of type bool - Meilleures réponses
- Rying to access array offset on value of type null - Meilleures réponses
- Notice gratuite - Guide
- Cannot access offset of type string on string - Forum PHP
- Montre mingrui notice - Forum Accessoires & objets connectés
- Input signal out of range ✓ - Forum Matériel & Système
- Qwerty to azerty - Guide
6 réponses
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
class Connect {
const SERVERNAME = "xxxxxx";
const DBNAME = "xxxxxx";
const DBUSER = "xxxxxxxx";
const DBPASS = "xxxxxxxxxxxxxx";
public function setConnection(){
try
{
$bdd = new PDO('mysql:host='.self::SERVERNAME.';dbname='.self::DBNAME.';charset=utf8',self::DBUSER,self::DBPASS);
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
return $bdd;
}
}
?>
Une solution possible pour afficher l'exception en cas d'erreur :
public function nextEv(){ try { // Récupération des données du prochain événement $req = $this->connect->query('SELECT id, name_ev, date_ev FROM event WHERE date_ev > CURRENT_DATE ORDER BY date_ev ASC LIMIT 1'); $data = $req->fetch(); $id = $data['id']; $req2 = $this->connect->prepare("SELECT COUNT(id) as nbr_participant FROM participant WHERE id_event = :id AND particip = 'oui'"); $req2->bindParam(':id', $id, PDO::PARAM_INT); $req2->execute(); $data2 = $req2->fetch(); $particip = $data2['nbr_participant']; $nextEv = $data['name_ev']; $dateEv = $data['date_ev']; $res = ['dateEv' => $dateEv, 'nextEv' => $nextEv, 'particip' => $particip]; return $res; } catch (Exception $e){ // en cas d'erreur : echo 'Erreur SQL : ' . $e->getMessage(); return false; } }Je retourne également la valeur false en cas d'erreur, à voir si cela convient avec le code qui utilise la fonction nextEv() : on pourrait éventuellement retourner un tableau vide à la place de false, ou remonter l'exception (via throw) afin de l'intercepter dans le code qui utilise la fonction nextEv() (via try/catch).