Page 1 of 1

Problems with Variable Scope using Include_Once

Posted: Wed Sep 20, 2006 11:07 am
by playuk
Hello,

I am fairly new to programming PHP, however I have knowledge of a few other languages.

I am sure that there is a simple solution to this problem, but I may just be unable to see it.

Details about my setup...
PHP Version: 5.0.0
Display Errors: On
Error Level: E_ALL
Register Globals: Off

The Problem...
My thought is that the problem is to do with the scope of the a variable, but I have read the PHP article on variable scope and I am still facing the same problem.

I have a main file (index.php) and a config file (config.inc.php) both in site root. I also have a connections file (connection.inc.php) in an include folder below the root.

I am trying to include the config file to set all the site variables and then include the connections file to create a connection object for use by the rest of the index page. I am getting an error "Undefined variable: link" from index.php. The code below shows the code snippet for each file...

config.inc.php

Code: Select all

$CONF = array ();

// Site Details - Name and URL
$CONF["Site_URL"] = 'http://localhost/test/';
$CONF["Site_Name"] = "Site Name";
index.php

Code: Select all

require_once("config.inc.php");
require_once($CONF["Site_URL"] . "include/connection.inc.php");
$sqlCommand = "SELECT intMenuItemID, varMenuText FROM menu";
$result = mysql_query($sqlCommand, $link);
connection.inc.php

Code: Select all

$host = "localhost";
$user = "php";
$password = "php";
$database = "test";
	
$link = mysql_connect($host, $user, $password) or die("Connection to database could not be made");
mysql_select_db($database, $link);
I am pretty sure that with this code, the reason for the error is due to the fact that the variable is defined in the included file and therefore that is where the scope of the variable ends. From reading the variable scope document, my understanding was that if the keyword "global" was added to the definition inside the included file, it would make the variable accessible in the index.php page.

As this does not work, I would appreciate some help in this matter.

Many thanks :)

Posted: Wed Sep 20, 2006 11:12 am
by volka
A file is not a scope boundary. An include works almost as if the code in the included file stands at the position of the include() statement.

Is either
$link = mysql_connect($host, $user, $password) or die("Connection to database could not be made");
mysql_select_db($database, $link);
or
$link = mysql_connect($host, $user, $password) or die("Connection to database could not be made");
mysql_select_db($database, $link);
within a function/method?

Posted: Wed Sep 20, 2006 11:13 am
by onion2k
If you use require_once() on a file starting with "http://" it'll open the file through an http wrapper, essentially the same sort of thing as if you'd requested http://www.google.com or ftp://www.mywebsite.co.uk. You need to use require_once() using a local filesystem name, eg ./includes/connection.inc.php.

Posted: Wed Sep 20, 2006 11:29 am
by playuk

Code: Select all

$link = mysql_connect($host, $user, $password) or die("Connection to database could not be made");
mysql_select_db($database, $link);
There is no function / method defined within this file.

I have tried giving the include_once a local filesystem path instead, but I face the same problem.

Posted: Thu Sep 21, 2006 5:17 am
by playuk
OK, thanks for the help...

For some reason I think my local install of PHP and apache was having a funny 5 minutes because when I have run the same code today, it is now working...

That is computers for you :)