Problème Foreach

Par GlobeVer9, il y a 9 ans


Bonjour,

Je rencontre actuellement un problème avec mon foreach : il n'affiche rien.
Si quelqu'un peut m'éclairé merci.

Voici mon code :
La page du foreach:

<?php require_once 'produit.php'; require_once 'ManagerProduit.php'; $affiche = new ManagerProduit(); $affiche->AffichBDD(); if(!is_object($affiche)) { echo "Non object"; } foreach($affiche->AffichBDD() as $attribut): echo $attribut['nomProd']; endforeach;

La page produit.php:

class produit { private $idProd; private $nomProd; private $descriptionProd; private $imgProd; private $categProd; public function setIdProd($idProd){ $this->idProd = $idProd; return $this; } public function getIdProd(){ return $this->idProd; } public function setNomProd($nomProd){ $this->nomProd = $nomProd; return $this; } public function getNomProd(){ return $this->nomProd; } public function setDescriptionProd($descriptionProd){ $this->descriptionProd = $descriptionProd; return $this; } public function getDescriptionProd(){ return $this->descriptionProd; } public function setImgProd($imgProd){ $this->imgProd = $imgProd; return $this; } public function getImgProd(){ return $this->imgProd; } public function setCategProd($categProd){ $this->categProd = $categProd; return $this; } public function getCategProd(){ return $this->categProd; } }

La page ManagerProduit.php:

require_once 'produit.php'; class ManagerProduit { private $c; private function setConnection(){ $this->c = new PDO('mysql:host=localhost;bdname=###','root',''); } public function __construct() { $this->setConnection(); } public function AffichBDD() { $afficher = $this->c->prepare('SELECT * FROM produit WHERE categProd="1"'); $afficher->execute(); $listes = $afficher->fetchAll(); return $listes; } }

La base de donnée:
PRODUIT(idProd(clé primaire), nomProd, descriptionProd, imgProd, categProd#)
CATEGORIE(idCateg(clé primaire), nomCateg, descriptionCateg)

La requete fonctionne elle a été testée dans phpmyadmin

2 réponses

SLK, il y a 9 ans

Salut,

quand tu fais ton new PDO,
il y a 3 "#" à la place du nom de la base de données,
et tu as mis 'bdname' au lieu de 'dbname'.

GlobeVer9, il y a 9 ans

Merci beaucoup c'était bien le bdname qui me posait problème :)