By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,641 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,641 IT Pros & Developers. It's quick & easy.

variable problem...

P: n/a
os2
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?
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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/
Jul 17 '05 #2

P: n/a
On Tue, 27 Apr 2004 14:45:37 +1200
Chris Hope <ch***@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);
}

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.