This is a discussion on variable problem... within the PHP Language forums, part of the PHP Programming Forums category; hi in bd.php i do: <?php $serveur = "localhost"; $utilisateur = "root"; $motDePasse = ""; $base = &...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
hi
in bd.php i do: <?php $serveur = "localhost"; $utilisateur = "root"; $motDePasse = ""; $base = "laboiteaprog"; $link = ""; function connectionbd() { //ouvre une connection $link = mysql_connect($serveur, $utilisateur, $motDePasse); mysql_select_db($base) or die("Connexion ratée"); } ?> don't get error in news.php i do: <?php include("bd.php"); .... ?> in news.php i get this error: Undefined variable: serveur Undefined variable: utilisateur Undefined variable: motDePasse Undefined variable: base .... any idea to resolve that? |
|
|||
|
os2 wrote:
> in bd.php i do: > > <?php > > $serveur = "localhost"; > $utilisateur = "root"; > $motDePasse = ""; > $base = "laboiteaprog"; > $link = ""; > > function connectionbd() > { > //ouvre une connection > $link = mysql_connect($serveur, $utilisateur, $motDePasse); > mysql_select_db($base) or die("Connexion ratée"); > } > ?> > > don't get error > > > in news.php i do: > > <?php > > include("bd.php"); > ... > ?> > in news.php i get this error: > > Undefined variable: serveur > Undefined variable: utilisateur > Undefined variable: motDePasse > Undefined variable: base > ... > > any idea to resolve that? Are you calling the connectionbd() from news.php? If so, then the problem is you are calling the function but the function has a whole load of undefined variables in it. Although you defined $serveur, $utilisateur, $motDePasse and $base outside the function they are not actually available inside the function unless you pass them as parameters, eg: function connectionbd($serveur, $utilisateur, $motDePasse, $base) { //ouvre une connection $link = mysql_connect($serveur, $utilisateur, $motDePasse); mysql_select_db($base) or die("Connexion ratée"); } In news.php these are actually already defined as you've called bd.php and the variables are set at the beginning of the script, so you could easily call connectiondb from any other page like so: include('bd.php'); connectionbd($serveur, $utilisateur, $motDePasse, $base); HTH -- Chris Hope The Electric Toolbox - http://www.electrictoolbox.com/ |
|
|||
|
On Tue, 27 Apr 2004 14:45:37 +1200
Chris Hope <chris@electrictoolbox.com> wrote: > os2 wrote: > > > in bd.php i do: > > > > <?php > > > > $serveur = "localhost"; > > $utilisateur = "root"; > > $motDePasse = ""; > > $base = "laboiteaprog"; > > $link = ""; > > > > function connectionbd() > > { > > //ouvre une connection > > $link = mysql_connect($serveur, $utilisateur, $motDePasse); > > mysql_select_db($base) or die("Connexion ratée"); > > } > > ?> > > > > don't get error > > > > > > in news.php i do: > > > > <?php > > > > include("bd.php"); > > ... > > ?> > > in news.php i get this error: > > > > Undefined variable: serveur > > Undefined variable: utilisateur > > Undefined variable: motDePasse > > Undefined variable: base > > ... > > > > any idea to resolve that? > > Are you calling the connectionbd() from news.php? If so, then the > problem is you are calling the function but the function has a whole > load of undefined variables in it. Although you defined $serveur, > $utilisateur, $motDePasse and $base outside the function they are > not actually available inside the function unless you pass them as > parameters, eg: > > function connectionbd($serveur, $utilisateur, $motDePasse, $base) > { > //ouvre une connection > $link = mysql_connect($serveur, $utilisateur, $motDePasse); > mysql_select_db($base) or die("Connexion ratée"); > } Or tell the function to work with global variables function func() { global $x, $y, $z; do_something($x,$y,$z); } |