Problème array empty
Résolu/Fermé
blackbutterfly
Messages postés
95
Date d'inscription
mardi 17 avril 2012
Statut
Membre
Dernière intervention
29 mai 2012
-
17 avril 2012 à 13:32
blackbutterfly Messages postés 95 Date d'inscription mardi 17 avril 2012 Statut Membre Dernière intervention 29 mai 2012 - 18 avril 2012 à 15:34
blackbutterfly Messages postés 95 Date d'inscription mardi 17 avril 2012 Statut Membre Dernière intervention 29 mai 2012 - 18 avril 2012 à 15:34
A voir également:
- Problème array empty
- Remove empty directories - Télécharger - Nettoyage
- Array mac - Guide
- Trying to access array offset on value of type bool ✓ - Forum PHP
- Param filters must be an array ✓ - Forum Programmation
- Empty folder finder - Télécharger - Bureautique
3 réponses
Juste te faire une proposition d'utiliser la gestion des erreurs PDO afin de tenter de voir pourquoi l'array est vide. Je te laisse consulter la doc officielle PHP concernant ce chapitre pour mieux l'appréhender...
http://php.net/manual/fr/pdo.error-handling.php
http://php.net/manual/fr/pdo.error-handling.php
blackbutterfly
Messages postés
95
Date d'inscription
mardi 17 avril 2012
Statut
Membre
Dernière intervention
29 mai 2012
1
17 avril 2012 à 14:02
17 avril 2012 à 14:02
Voici la modification pour augmenter le niveau de debug :
public function Get_MP_info ()
{
define ('USER', 'root@localhost');
define ('Paswd', '');
try {
$pdo_opt[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$pdo = new PDO('mysql:host=localhost;dbname=test', USER, Paswd, $pdo_opt);
$sql = "SELECT ID_PRESTA, CODE, LIBELLE, PVHT, TVA, PVTTC FROM PRESTATION";
$stmt = $pdo->prepare($sql);
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($rs);
print_r($rs);
$stmt->closeCursor();
return $rs;
}
catch (PDOException $err) {
$msg='DB -> '.$err->getMessage();
echo $msg;
}
}
Je n'ai aucun nouveau résultat d'affiché. -_- hormis mes :
array
empty
Array ( )
( ! ) Notice: Undefined index: CODE
( ! ) Fatal error: Cannot use object of type MProduit as array in
Qui me troll depuis maintenant 4h ... Je suis a bout
public function Get_MP_info ()
{
define ('USER', 'root@localhost');
define ('Paswd', '');
try {
$pdo_opt[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$pdo = new PDO('mysql:host=localhost;dbname=test', USER, Paswd, $pdo_opt);
$sql = "SELECT ID_PRESTA, CODE, LIBELLE, PVHT, TVA, PVTTC FROM PRESTATION";
$stmt = $pdo->prepare($sql);
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($rs);
print_r($rs);
$stmt->closeCursor();
return $rs;
}
catch (PDOException $err) {
$msg='DB -> '.$err->getMessage();
echo $msg;
}
}
Je n'ai aucun nouveau résultat d'affiché. -_- hormis mes :
array
empty
Array ( )
( ! ) Notice: Undefined index: CODE
( ! ) Fatal error: Cannot use object of type MProduit as array in
Qui me troll depuis maintenant 4h ... Je suis a bout
blackbutterfly
Messages postés
95
Date d'inscription
mardi 17 avril 2012
Statut
Membre
Dernière intervention
29 mai 2012
1
17 avril 2012 à 14:33
17 avril 2012 à 14:33
Si pourtant la requête que je lance sur la console mysql me renvoie bien tous les résultats
blackbutterfly
Messages postés
95
Date d'inscription
mardi 17 avril 2012
Statut
Membre
Dernière intervention
29 mai 2012
1
17 avril 2012 à 14:55
17 avril 2012 à 14:55
J'ai modifié ma fonction comme cela:
public function Get_MP_info ()
{
try {
// On se connecte à MySQL
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '', $pdo_options);
$reponse = $bdd->query('SELECT ID_PRESTA, CODE, LIBELLE, PVHT, TVA, PVTTC FROM PRESTATION');
$donnees = $reponse->fetchAll();
//print_r($donnees);
$reponse->closeCursor(); // Termine le traitement de la requête
return ($donnees);
}
catch(Exception $e)
{
// En cas d'erreur précédemment, on affiche un message et on arrête tout
die('Erreur : '.$e->getMessage());
}
}
et quand je fais :
$List_Presta = $produit->Get_MP_info();
print_r($List_Presta);
voici ce qui s'affiche :
Array ( [0] => Array ( [ID_PRESTA] => 1 [0] => 1 [CODE] => 01A [1] => 01A ..........
Pourtant, il continu à me sortir les erreurs suivantes :
( ! ) Notice: Undefined index: CODE pour la ligne : $nb = sizeof($List_Presta['CODE']);
( ! ) Notice: Undefined index: ID_PRESTA pour la ligne : echo ($List_Presta['ID_PRESTA']);
Si quelqu'un pense avoir la solution ...
public function Get_MP_info ()
{
try {
// On se connecte à MySQL
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '', $pdo_options);
$reponse = $bdd->query('SELECT ID_PRESTA, CODE, LIBELLE, PVHT, TVA, PVTTC FROM PRESTATION');
$donnees = $reponse->fetchAll();
//print_r($donnees);
$reponse->closeCursor(); // Termine le traitement de la requête
return ($donnees);
}
catch(Exception $e)
{
// En cas d'erreur précédemment, on affiche un message et on arrête tout
die('Erreur : '.$e->getMessage());
}
}
et quand je fais :
$List_Presta = $produit->Get_MP_info();
print_r($List_Presta);
voici ce qui s'affiche :
Array ( [0] => Array ( [ID_PRESTA] => 1 [0] => 1 [CODE] => 01A [1] => 01A ..........
Pourtant, il continu à me sortir les erreurs suivantes :
( ! ) Notice: Undefined index: CODE pour la ligne : $nb = sizeof($List_Presta['CODE']);
( ! ) Notice: Undefined index: ID_PRESTA pour la ligne : echo ($List_Presta['ID_PRESTA']);
Si quelqu'un pense avoir la solution ...
blackbutterfly
Messages postés
95
Date d'inscription
mardi 17 avril 2012
Statut
Membre
Dernière intervention
29 mai 2012
1
17 avril 2012 à 15:10
17 avril 2012 à 15:10
Problème résolue : echo ($List_Presta['1']['1']);
vordano
Messages postés
1685
Date d'inscription
mardi 12 juillet 2011
Statut
Membre
Dernière intervention
20 juillet 2012
316
17 avril 2012 à 16:02
17 avril 2012 à 16:02
une question: est ce que l'utilisation de
$stmt->execute();sur ton premier bout de code aurais pus régler le problème ?
blackbutterfly
Messages postés
95
Date d'inscription
mardi 17 avril 2012
Statut
Membre
Dernière intervention
29 mai 2012
1
18 avril 2012 à 15:34
18 avril 2012 à 15:34
Je pense. Le problème est qu'il n'effectuait pas la requête ( j'en suis persuadé ) donc je pense que oui mais, je ne sais pas.