variable problem

This is a discussion on variable problem within the alt.comp.lang.php 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 > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-27-2004
Marc Collin
 
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?


--
Borland rulez http://pages.infinit.net/borland
Reply With Quote
  #2 (permalink)  
Old 04-27-2004
®ed Eye Media - Richard Grove
 
Posts: n/a
Default Re: variable problem


"Marc Collin" <os2@videotron.ca> wrote in message
news:2Njjc.44481$Tu2.945553@weber.videotron.net...
> 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?
>
>



Either globalize the variables inside the function.
OR call the function like this:

connectionbd ($serveur, $utilisateur, $motDePasse, $base, $link)

Hope this helps
RG



Reply With Quote
  #3 (permalink)  
Old 04-28-2004
Rob
 
Posts: n/a
Default Re: variable problem


"Marc Collin" <os2@videotron.ca> schreef in bericht
news:2Njjc.44481$Tu2.945553@weber.videotron.net...
> 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?
>
>
> --
> Borland rulez http://pages.infinit.net/borland



Marc ,

I do not think you get the eror when including the bd.php. You probably get
the error when calling the function connectionbd() definend in bd.php.

The variables you are declaring in bd.php will be invisible inside the
function connectionbd(). To solve this problem declare the variables inside
the function or pass them as parameters to the function or declare them as
global variables or create a class which wil take care of the connection.

Also read http://nl3.php.net/variables.scope for more information about the
subject

HTH
Rob






Reply With Quote
Reply


Thread Tools
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

vB 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 11:15 AM.


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