Jump to content

Archived

This topic is now archived and is closed to further replies.

Rich464

variable scope problem

Recommended Posts

I was wondering if someone could help me with what is likely a simple variable scope problem.

as per my code below, variables declared in an include file arent available inside functions that I declare the include from. Simplified version is below. Now I've read the PHP manual and searched the net on variable scope and I understand most of it, but none seem to cover this exact situation.

It's a silly thing, and if someone could help me with it I'd really appreciate it, I'm using php 5 and MySQL 5

[code]config.php
<?php

$user = "user";
$pass = "pass";

?>


display.php
<?php

include ('config.php');

echo $user; //these seem to work fine
echo $pass; //values are displayed correctly

function display_vars()
{
  echo $user; //these give undefined variable warnings
  echo $pass; //have tried using global too
}

display_vars() 

?>[/code]

Cheers in advance  :-*

Share this post


Link to post
Share on other sites
ok is there a link to this page?

try
config.php
[code]<?php

$user = "user";
$pass = "pass";

?>[/code]


display.php
[code]<?php

include ('config.php');

echo $user; //these seem to work fine
echo $pass; //values are displayed correctly

function display_vars()
{
  echo $user; //these give undefined variable warnings
  echo $pass; //have tried using global too
}

display_vars();

?>[/code]

i changed display_vars() to display_vars();

Share this post


Link to post
Share on other sites
If you use [tt]global $user, $pass;[/tt] at the beginning of display_vars() now (after including config.php), it should work.

Share this post


Link to post
Share on other sites
no,sorry.

Man I really screwed this up trying to simplify it. Here I'll post the actual code.

[code]
config.php
<?php

$hostname = "Localhost"; //the hostname for the database server

$username = "user"; //The username to access the database with

$password = "password"; //the password to use to access the database

$dbname = "db name"; //the actual name of the database to use

?>

dbconnect.php
<?php

require ('config.php');

function db_connect()
{
//use values pulled from config.php
$db = new mysqli($hostname,$username,$password,$dbname);
return $db;
}

$conn = db_connect();

?>

[/code]

the point is when the db_connect() function is called I get undefined variable warnings. I need to figure out why, or some way around it

Why aren't the variables declared in the include file available inside the function?

Share this post


Link to post
Share on other sites
try

config.php
[code]<?php

$hostname = "Localhost"; //the hostname for the database server

$username = "user"; //The username to access the database with

$password = "password"; //the password to use to access the database

$dbname = "db name"; //the actual name of the database to use

?>[/code]

dbconnect.php
[code]<?php

require ('config.php');

function db_connect()
{
        global $hostname, $username, $password;
//use values pulled from config.php
$db = mysql_connect($hostname,$username,$password);
        if (!$db) die('Could not connect:' . mysql_error());
return $db;
}

$conn = db_connect();

mysql_select_db($dbname, $conn)
or die('Could not open database:' . mysql_error());
?>[/code]

why not just do
dbconn.php
[code]
<?php

$mysql_location = 'localhost';
$mysql_user = 'user';
$mysql_password = 'pass';
$mysql_db = 'database';

$link = mysql_connect($mysql_location, $mysql_user, $mysql_password);
if (!$link) die('Could not connect:' . mysql_error());

mysql_select_db($mysql_db, $link)
or die('Could not open database:' . mysql_error());

?>
[/code]


you can just do include 'dbconn.php'; on each page, much simpler

Share this post


Link to post
Share on other sites
Well I managed to gather what I needed to do from that, as your code still reports $dbname as an undefined variable.

It works when the variables are declared as global INSIDE the function.....weird.

Anyway thanks for your help  :) :D

Share this post


Link to post
Share on other sites
[quote]It works when the variables are declared as global INSIDE the function.....weird.[/quote]
That's how global works in PHP.

Ken

Share this post


Link to post
Share on other sites
What you really need to be doing is

[code]
function display_vars($username, $password)
{
  echo $username; //these give undefined variable warnings
  echo $password; //have tried using global too
}

display_vars($user, $pass)
[/code]

This is a far more structured way than using global variables.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.