variable problem...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • os2

    variable problem...

    hi

    in bd.php i do:

    <?php

    $serveur = "localhost" ;
    $utilisateur = "root";
    $motDePasse = "";
    $base = "laboiteapr og";
    $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?
  • Chris Hope

    #2
    Re: variable problem...

    os2 wrote:
    [color=blue]
    > in bd.php i do:
    >
    > <?php
    >
    > $serveur = "localhost" ;
    > $utilisateur = "root";
    > $motDePasse = "";
    > $base = "laboiteapr og";
    > $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?[/color]

    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($s erveur, $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($s erveur, $utilisateur, $motDePasse, $base);

    HTH

    --
    Chris Hope
    The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • Kelly Thompson

      #3
      Re: variable problem...

      On Tue, 27 Apr 2004 14:45:37 +1200
      Chris Hope <chris@electric toolbox.com> wrote:
      [color=blue]
      > os2 wrote:
      > [color=green]
      > > in bd.php i do:
      > >
      > > <?php
      > >
      > > $serveur = "localhost" ;
      > > $utilisateur = "root";
      > > $motDePasse = "";
      > > $base = "laboiteapr og";
      > > $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?[/color]
      >
      > 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($s erveur, $utilisateur, $motDePasse, $base)
      > {
      > //ouvre une connection
      > $link = mysql_connect($ serveur, $utilisateur, $motDePasse);
      > mysql_select_db ($base) or die("Connexion ratée");
      > }[/color]

      Or tell the function to work with global variables

      function func() {
      global $x, $y, $z;
      do_something($x ,$y,$z);
      }

      Comment

      Working...