php 如何在各自的php文件中包含db配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3175411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:55:19  来源:igfitidea点击:

how to include db config file in respective php files

phpmysqldatabase

提问by Mac Taylor

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

我在编码中的主要问题之一是包括 config.php 和 db 类,我需要在其中连接到数据库

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

考虑我的文件根目录中有一个 mysql.php 和 config.php 文件

now if im in this path :

现在如果我在这条路上:

Root/Sub/portal/modules/Gallery.php

根/子/门户/模块/Gallery.php

and i need to fetch config.php vars such as

我需要获取 config.php 变量,例如

$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


警告:包含(../../../config.php)[function.include]:无法打开流:没有这样的文件或目录


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

那么有什么方法可以避免此错误并找到连接到我的数据库的方法?!

回答by Emre Yazici

Use this:

用这个:

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

回答by Gordon

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

实际上,最简单的方法是从现有的包含路径中包含

For instance, if your include_pathis set to

例如,如果您的include_path设置为

/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_pathto 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.

就像kgb 已经建议的那样,您可以修改 include_path以保存 PHP 查找文件的其他路径。但是,您应该使用合理数量的路径。如果您有一堆其他包含所需文件的文件夹,那么设置配置文件夹的路径只是为了能够包含“mysql.txt”是不明智的。Web 应用程序中的一种常见方法是设置应用程序根文件夹的路径。

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.

我想到了许多其他存储应用程序路径的方法,但我想以上内容足以解决您迫在眉睫的问题。

回答by Sergey Eremin

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

您可以在 php.ini 文件中扩展 php 包含路径或使用set_include_path()

set_include_path(get_include_path().PATH_SEPARATOR.$pathToMysqlPhp);
include('mysql.php');

回答by Toby Allen

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.

一个低技术的解决方案,但它对我有用。

回答by Sail

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.

使用它来定义项目中的 php 源。和 ./ 它是我们文件的基本路径。