views:

389

answers:

5

Alright, so this is something really basic, and I know that when someone tells me the answer I'm going to feel really silly, but I can't for the life of me figure out why the following code isn't working:

index.php:

<?php include('config.php'); //this works fine. variables in this file are reached correctly - $_MYSQL is defined there ?>

<?php include($_MYSQL); ?>
<?php echo ($fruit);?>

db_config.php (which is what $_MYSQL links to, this works no problem):

<?php 

$fruit = "apple";
echo($fruit);
?>

for completeness config.php looks like:

<?php 

$_MYSQL = 'http://'.$_SERVER['HTTP_HOST'].'/public_html/db_config.php';

$BASE_URL = 'http://'.$_SERVER['HTTP_HOST'].'/public_html/Opto10/';

?>

So as the names imply the code is meant to contact this db_config.php that then connects to the database, but for some reason the variables in the db_config file don't seem to carry across to index.php. The weird thing is that include('config.php'); works perfectly fine, but in the code I've shown above in the index.php "echo($fruit);" doesn't print out anything. The same line in db_config.php does though (so I guess that it does mean it's included). Somehow the variables aren't passed along. Just so you know, in case this makes a difference, the db_config.php file is located in the parent of the current directory.

I'm thoroughly puzzled, any help is extremely welcome. Thanks in advance,

Simon

What you have abovev is literally all my php code.

A: 

if the connection to DB fails, you have false in the variable, so echo false; is like echo ''; --> you'll see nothing.

Try the var_dump($variable); on $fruit, as Manzoo Ahmed suggested to you.

mere-teresa
A: 
<?php
$_MYSQL = "db_config.php';
?>

or just change the include to

<?php
include('db_config.php');
?>
Paul Janaway
Taking into consideration the moving up a directory this did fix it. Why couldn't I use a direct link?
Simon
You can't supply the url of the file eg. http://blah.com/inc/config.php if you wished to include this from the base url you would have to include inc/config.php
Paul Janaway
+4  A: 

You can't inlcude across HTTP like that

From the manual

If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.

Peter Bailey
A: 

In index.php

<?php

defined("BASEDIR", dirname(__FILE__));
defined("DIRMYSQL", BASEDIR . "/public_html/db_config.php");

?>
andres descalzo
A: 

You are trying to include a web page... which you cant do.

include('http://some_domain/public_html/db_config.php') aint going to work...

you want to include a file in a directory:

include('C:/some_directory/public_html/db_config.php')

Bingy