views:

83

answers:

4

hey guys

one of my main problems in coding is including config.php and db class where i need to connect to databse

consider i have a mysql.php and config.php file in root of my files

now if im in this path :

Root/Sub/portal/modules/Gallery.php

and i need to fetch config.php vars such as

$dbhost = "localhost"; 
$dbuname = "root";  // Database username
$dbpass = "";   // Database password
$dbname = "mydb";   // Database NAME

i should include it in this way :

require("../../../config.php");
require("../../../mysql.php");

$mdb = new sql_db($dbhost, $dbuname, $dbpass, $dbname, false);
if(!$mdb->db_connect_id) {
die("cant connect to db");
}

sure it would give me an error :

Warning: include(../../../config.php) [function.include]: failed to open stream: No such file or directory in


so is there any way to avoid this error and find a way to connect to my db ?!

+1  A: 

you can extend the php include path in php.ini file or using set_include_path():

set_include_path(get_include_path().PATH_SEPARATOR.$pathToMysqlPhp);
include('mysql.php');
kgb
it's a file system path to mysql.php. like /var/www/projects/proj1/mysql.php
kgb
it's a good idea to do it in the beginning of index.php. configuration info path(like your mysql.php) usually doesn't change. this way you won't need do include it in many files or change it very often.
kgb
+3  A: 

Use this:

require(dirname(__FILE__)."/../../../config.php");
require(dirname(__FILE__)."/../../../mysql.php");
eyazici
@Mac Taylor: It means there is no file named config.php under D:\xampp\htdocs\ directory.
eyazici
"/" is also a valid directory separator for Windows, so it's not the case. AS of "../", it will automatically be resolved by OS to the right path.
eyazici
+1  A: 

Actually, the easiest would be to just include from the existing include path.

For instance, if your include_path is set to

/some/path 

and your config file is located in

/some/path/app/config/mysql.txt

you can just do

include 'app/config/mysql.txt'

Like kgb already suggested, you can modify the include_path to hold additional paths for PHP to look in for files. However, you should use a sensible amount of pathes. Setting the path to the config folder just to be able to do include 'mysql.txt' is not sensible if you got a bunch of other folders with required files. A common approach in web applications is setting the path to the application root folder.

If you are bootstrapping your application, you can also set a constant that sets the application path. I am not a fan of adding constants to the global namespace, but it's something you see often too. You would then do

include APP_ROOT . '/config/mysql.txt';

A number of other approaches for storing the application path come to my mind, but I guess the above is sufficient to solve your imminent question.

Gordon
and if u mind ! what that means :D include from the include path , any example ?!
Mac Taylor
@MacTaylor that's not the opposite :) use `get_include_path()` to see what your current `include_path` is set to. Then either include relative from one of those paths or - if that isn't possible - add an appropriate path (like kgb suggested) or use the constant approach.
Gordon
@MacTaylor just for clearification: if your file is in `/some/path/app/index.php` and the other is in `/some/other/config.php` and your include_path is set to `/some` you can `include('other/config.php')` from `/some/path/app/index.php`. That's why it's not the opposite, but just a matter of what the include_path is. You can always include from the include_path regardless of the actual location of the file the include is happening in.
Gordon
A: 

I came across a similar problem when using a templating system a few years ago. I solved it by ensuring that all sets of files are the same depth down the filepath

eg

www.mywebsites.com/pages/myfile.php
www.mywebsites.com/templates/mytemplate.html
www.mywebsites.com/images/image.png
www.mywebsites.com/includes/myfunctionfile.php

this way when including a file from any other file you just use a path relative to the root,

include('../includes/myfunctionfile.php');

a low tech solution, but it works for me.

Toby Allen