variable problem...

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 = &...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-27-2004
os2
 
Posts: n/a
Default variable problem...

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?
Reply With Quote
  #2 (permalink)  
Old 04-27-2004
Chris Hope
 
Posts: n/a
Default Re: variable problem...

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/
Reply With Quote
  #3 (permalink)  
Old 04-30-2004
Kelly Thompson
 
Posts: n/a
Default Re: variable problem...

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);
}

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:30 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0