2014-12-18 11:36
浏览 44


I'm trying to connect to my database using a function I've used in a previous project. The only thing I've changed is, I've added a loader.php file.


I've made this loader to include my files, starting from the root path, because I was getting some issues with relative pathing when working with multiple pages in different directories.


function includeFile($path) {
    include ( $_SERVER['DOCUMENT_ROOT'] . '/include/'.$path);

function requireFile($path) {
    require ( $_SERVER['DOCUMENT_ROOT'] . '/include/'.$path);   

function includeFileOnce($path) {
    include_once ( $_SERVER['DOCUMENT_ROOT'] . '/include/'.$path);   



 * database login details
define("HOST", "localhost");
define("USER", "****"); 
define("PASSWORD", "****");
define("DATABASE", "****");

define("SECURE", FALSE); //development purposes


include_once dirname(__FILE__) . '\db_config.php';
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);


include_once dirname(__FILE__) . '\db_connect.php';

function getUrl($mysqli) {
    if($stmt = $mysqli->prepare("SELECT english, dutch FROM url_translation")) { //line 5

        $stmt->bind_result($english, $dutch);

        echo $english . "<br>";
        echo $dutch . "<br>";

examplePage.php When just using include() everything works and I'm getting results from the query. But when I'm using includeFile(); I'm getting 2 errors that I've listed below.

include( $_SERVER['DOCUMENT_ROOT'] . '/loader.php' );
//include('../backend/url_translator.php'); //works
includeFile('backend/url_translator.php'); //function from loader.php doesn't work

            getAllUrl($mysqli); //line 12

error when visiting examplePage

Notice: Undefined variable: mysqli in C:\xampp\htdocs\projectname\en\examplePage.php on line 12

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\projectname\include\backend\url_translator.php on line 5

It is possible to fix the first error by adding global $mysqli; at url_translator.php. But I've passed $mysqli through a parameter.

Why does it react differently when using a function to include files instead of just using include()?

All I can think about that it might be a scoping problem, but I have not a clue on how to fix it.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douju2599 2014-12-18 12:00

    Can you echo the value of $_SERVER['DOCUMENT_ROOT'] . '/include/' . $path when you call includeFile() in examplePage.php?

    点赞 评论

相关推荐 更多相似问题