468,133 Members | 1,240 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

Defining variables in a seperate file

I am missing a trick here on how use variables defined in seperate file.

In script 1, I have proved to myself my class is working OK because I can
pass in the values and the echo statement outputs the value of localhost.

<?php
// Script 1
// This script works and outputs the name of localhost
require_once("class.MySQL.php");
$db = new MySQL("localhost", "user", "password", "database");
echo "Name of host: ".$db->host;
?>

However if I assign these values in a seperate file called configuration.php
and put the $variable names in the instantiation statement, it doesn't seem
to work. Can't understand why it doesn't work because I reckon it should
still be passing in the values of $localhost, $user, $password and $database
which is exactly as script 1.

<?php
/* Script 2
This script does not seem assign a value to $localhost
Here is the contents of config.php without the open/close PHP tags.
$host = "localhost";
$user = "user";
$password = "password";
$database = "database"; */

require_once("configuration.php");
require_once("class.MySQL.php");
$db = new MySQL($localhost, $user, $password, $database);
echo "Name of host: ".$db->host;
?>

Appreciate any comments. I am sure I have overlooked something very obvious.

Cheers

Phil

May 23 '06 #1
6 1475
Rik
One of two?
$host = "localhost";
$db = new MySQL($localhost..... Here is the contents of config.php
require_once("configuration.php");


Grtz,
--
Rik Wasmus
May 23 '06 #2
Rik
Rik wrote:
One of two?
$host = "localhost";
$db = new MySQL($localhost.....

Here is the contents of config.php
require_once("configuration.php");


BTW: this is one of the reasons I like to build code with E_NOTICE on: if
would have given me notive $localhost is undefined...

Grtz,
--
Rik Wasmus
May 23 '06 #3
ph********@f-in-stupid.co.uk says...
<?php
/* Script 2
This script does not seem assign a value to $localhost
Here is the contents of config.php without the open/close PHP tags.
$host = "localhost";


Maybe because you've called it $host not $localhost ??

GM
May 23 '06 #4

"Geoff Muldoon" <ge***********@trap.gmail.com> wrote in message
news:MP************************@news.readfreenews. net...
ph********@f-in-stupid.co.uk says...
<?php
/* Script 2
This script does not seem assign a value to $localhost
Here is the contents of config.php without the open/close PHP tags.
$host = "localhost";


Maybe because you've called it $host not $localhost ??

GM


Yes, typical I've been looking at this for 2 hours and 5 mins after posting
I spot my obvious mistake.

Thanks for taking the trouble.

Cheers

Phil
May 23 '06 #5
On 2006-05-23, Phil Latio <ph********@f-in-stupid.co.uk> wrote:

"Geoff Muldoon" <ge***********@trap.gmail.com> wrote in message
news:MP************************@news.readfreenews. net...
ph********@f-in-stupid.co.uk says...
> <?php
> /* Script 2
> This script does not seem assign a value to $localhost
> Here is the contents of config.php without the open/close PHP tags.
> $host = "localhost";


Maybe because you've called it $host not $localhost ??

GM


Yes, typical I've been looking at this for 2 hours and 5 mins after posting
I spot my obvious mistake.


Just add the following lines to your scripts (or change your php.ini)
while you're developping: (It really does make you write less mistakes)

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>
May 23 '06 #6
> Just add the following lines to your scripts (or change your php.ini)
while you're developping: (It really does make you write less mistakes)

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>


Thanks. That is a great tip, I just tried it on the file which I was
originally having problems with and the error message was very useful.

Cheers

Phil
May 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Mark Parnell | last post: by
13 posts views Thread by Larry L | last post: by
3 posts views Thread by Microsoft | last post: by
8 posts views Thread by Floris van Haaster | last post: by
42 posts views Thread by Dooglo | last post: by
13 posts views Thread by Řrjan Langbakk | last post: by
4 posts views Thread by Hans | last post: by
7 posts views Thread by Roman Mashak | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.