Jump to content


Photo

variable scope problem


  • Please log in to reply
10 replies to this topic

#1 Rich464

Rich464
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 21 July 2006 - 03:18 PM

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

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()  

?>

Cheers in advance  :-*

#2 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 21 July 2006 - 03:20 PM

in display.php do the foloowing
include 'config.php';


[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]

#3 Rich464

Rich464
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 21 July 2006 - 03:21 PM

oh bugger, sorry.

No I did put that (duh!)

sorry I'm an idiot.

ill amend it :P

#4 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 21 July 2006 - 03:24 PM

ok is there a link to this page?

try
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();

?>

i changed display_vars() to display_vars();

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]

#5 Oldiesmann

Oldiesmann
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationCincinnati, Ohio

Posted 21 July 2006 - 03:25 PM

If you use global $user, $pass; at the beginning of display_vars() now (after including config.php), it should work.
The Oldiesmann
SMF Project Manager
SMF+Gallery2 - Beta3.1 now available

#6 Rich464

Rich464
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 21 July 2006 - 03:29 PM

no,sorry.

Man I really screwed this up trying to simplify it. Here I'll post the actual 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();

?>


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?

#7 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 21 July 2006 - 03:35 PM

try

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()
{
        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());
?>

why not just do
dbconn.php
<?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());

?>


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

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]

#8 Rich464

Rich464
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 21 July 2006 - 03:39 PM

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



#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 21 July 2006 - 03:45 PM

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

That's how global works in PHP.

Ken

#10 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 21 July 2006 - 03:47 PM

yep always remember that, if your in a function use global

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]

#11 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 21 July 2006 - 04:04 PM

What you really need to be doing is

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

display_vars($user, $pass)

This is a far more structured way than using global variables.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users