开发者

how to include db config file in respective php files

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 开发者_JAVA技巧error and find a way to connect to my db ?!


Use this:

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


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.


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');


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.


include './common/class/config.php';

To use this to define the source of a php in project. and the ./ its a basic path of our file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜