Problème connexion à une BDD PDO
Résolu
elnoob
Messages postés
7
Date d'inscription
Statut
Membre
Dernière intervention
-
elnoob Messages postés 7 Date d'inscription Statut Membre Dernière intervention -
elnoob Messages postés 7 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
Je suis un novice en PHP (ex-developpeur COBOL).
J'ai crée une fichier qui gère mes fonctions, et un fichier qui liste les données que j'ai stocké dans une base.
Voici un extrait de mon fichier "fonction.php" :
<?php
function connexion()
{
try {
$bdd = new PDO('mysql:host=localhost;dbname=bibliotheque;charset=utf8', 'root', '');
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch (Exception $e){
die('Erreur : ' . $e->getMessage());
}
return $bdd;
echo 'retour de la valeur';
}
function ListeAuteur($a){
$sql = 'SELECT prenomAuteur,nomAuteur,descrAuteur FROM auteurs order by nomAuteur' ;
$reponse = $bdd->query($sql);
return $reponse;
}
?>
Voici un extrait du code de mon fichier "listeAuteur.php" :
<?php
require_once("../fct/connexionbdd.php");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/ccs_bib.css" rel="stylesheet" type="text/css">
</head>
<body>
<h3><strong>Auteur</strong></h3>
<?php
$tableau['prenom'] ='Prenom ';
$tableau['nom'] ='Nom ';
$tableau['description'] ='Description ';
connexion();
echo $tableau['nom']; echo $tableau['prenom']; echo $tableau['description'];
ListeAuteur($bdd);
print_r($reponse->fetchAll(PDO::FETCH_FOOBAR));
Je récupère ce message d'erreur :
Notice: Undefined variable: bdd in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\tpbiblio\admin\ListeAuteur.php on line 26
Notice: Undefined variable: bdd in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\tpbiblio\fct\connexionbdd.php on line 17
Fatal error: Call to a member function query() on a non-object in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\tpbiblio\fct\connexionbdd.php on line 17
Quelqu'un pourrait me dire où est-ce que je me suis trompé, svp ?
Je suis un novice en PHP (ex-developpeur COBOL).
J'ai crée une fichier qui gère mes fonctions, et un fichier qui liste les données que j'ai stocké dans une base.
Voici un extrait de mon fichier "fonction.php" :
<?php
function connexion()
{
try {
$bdd = new PDO('mysql:host=localhost;dbname=bibliotheque;charset=utf8', 'root', '');
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch (Exception $e){
die('Erreur : ' . $e->getMessage());
}
return $bdd;
echo 'retour de la valeur';
}
function ListeAuteur($a){
$sql = 'SELECT prenomAuteur,nomAuteur,descrAuteur FROM auteurs order by nomAuteur' ;
$reponse = $bdd->query($sql);
return $reponse;
}
?>
Voici un extrait du code de mon fichier "listeAuteur.php" :
<?php
require_once("../fct/connexionbdd.php");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/ccs_bib.css" rel="stylesheet" type="text/css">
</head>
<body>
<h3><strong>Auteur</strong></h3>
<?php
$tableau['prenom'] ='Prenom ';
$tableau['nom'] ='Nom ';
$tableau['description'] ='Description ';
connexion();
echo $tableau['nom']; echo $tableau['prenom']; echo $tableau['description'];
ListeAuteur($bdd);
print_r($reponse->fetchAll(PDO::FETCH_FOOBAR));
Je récupère ce message d'erreur :
Notice: Undefined variable: bdd in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\tpbiblio\admin\ListeAuteur.php on line 26
Notice: Undefined variable: bdd in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\tpbiblio\fct\connexionbdd.php on line 17
Fatal error: Call to a member function query() on a non-object in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\tpbiblio\fct\connexionbdd.php on line 17
Quelqu'un pourrait me dire où est-ce que je me suis trompé, svp ?
A voir également:
- Problème connexion à une BDD PDO
- Gmail connexion - Guide
- Connexion chromecast - Guide
- Gmail connexion autre compte - Guide
- D'où peut venir un problème de connexion internet sur un ordinateur ? - Guide
- Site inaccessible n'autorise pas la connexion - Guide
2 réponses
Quand vous faite return $bdd dans connexion() il faut la sauvegarder de l'autre coté quand vous applez la fonction :
$bdd = connexion();
$reponse = listeauteur($bdd);
En effet en php les variable que vous retournez doivent être réassigné dans une autre variable
Enfin tout ce que vous mettrez apres un "return $variable;"
ne sera jamais éxécuter
donc le "echo 'retour de la valeur'; "
ne sera pas éxecuter
$bdd = connexion();
$reponse = listeauteur($bdd);
En effet en php les variable que vous retournez doivent être réassigné dans une autre variable
Enfin tout ce que vous mettrez apres un "return $variable;"
ne sera jamais éxécuter
donc le "echo 'retour de la valeur'; "
ne sera pas éxecuter
function ListeAuteur($a){
$a = $bdd;
$sql = 'SELECT prenomAuteur,nomAuteur,descrAuteur FROM auteurs order by nomAuteur' ;
$reponse = $bdd->query($sql);
return $reponse;
}