1

I'm currently re-writing one of my old PHP scripts so that the code is well structured for future updates.

One thing I'm trying to do, is create a configurations file - so that settings can be easily changed by editing one single file.

source code of file: functions.php

function ConnectToDB() {

    //database configurations...
    $host = "localhost";
    $dbuser = "root";
    $dbpass = "";
    $tblname = "parents";

    $connection = mysql_connect($host,$dbuser,$dbpass);

    if (!$connection) {
        echo 'Could not connect to the database. Please contact the administrator for more information.';
    }

    // select the db
    $db_selected = mysql_select_db($tblname, $connection);

    if (!$db_selected) {
        echo 'Could not select the parents evening table. Please contact the administrator for more information.';
        return false;
    }else{
        return true;
    }

}

file: config.php

<?php

//database configurations...
$host = "localhost";
$dbuser = "root";
$dbpass = "";
$tblname = "parents";

?>

This is my new code: file: functions.php

function ConnectToDB() {

    include('inc/config.php');

    global $host; //needed becuase the scope of the variable throws an error otherwise.
    global $dbuser; //needed becuase the scope of the variable throws an error otherwise.
    global $dbpass; //needed becuase the scope of the variable throws an error otherwise.
    global $tblname; //needed becuase the scope of the variable throws an error otherwise.

    $connection = mysql_connect($host,$dbuser,$dbpass);

    if (!$connection) {
        echo 'Could not connect to the database. Please contact the administrator for more information.';
    }

    // select the db
    $db_selected = mysql_select_db($tblname, $connection);

    if (!$db_selected) {
        echo 'Could not select the parents evening table. Please contact the administrator for more information.';
        return false;
    }else{
        return true;
    }

}

My old code used to work perfectly, however now I've changed variables around to different files, my application keeps outputting "Could not select the parents evening table. Please contact the administrator for more information." - which means my application is not connecting to the database properly.

Does anyone have any ideas what's wrong with my code? I thought it was a scope issue at first, however I just can't find out what I've done wrong here.

Thanks in advance for any replies.

2 Answers 2

2

You should not need the global keyword. The include is defining those variables directly within the function. If the include was outside of the function, you would then need the global keywords.

Sign up to request clarification or add additional context in comments.

1 Comment

Fixed it now, thank you! :) I feel kinda silly for not figuring this out myself!
1

I'm not sure you need the global keyword if you've included your settings file as it is.

However, it's generally better practice to avoid global variables as much as possible. Consider putting the settings parameters into a Settings class as constants, so you would call them via Settings::host, etc.

Global variables as you have introduce a lot of potential bugs that can be hard to track down, and might well be the cause of your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.