Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Require_once() to include database connection variables correctly

I'm a php newbie (but long time developer in other languages) and I'm trying some example db connections in "PHP, MySQL, & JavaScript". It shows an example file to include db connection variables (servername, username, password, database, etc.). I have a php file which has a handful of functions I wrote and one of them has a few SQL queries. For whatever reason, calling require_once in that file doesn't output any errors (I have E_ALL config'd) yet those variables in my database php file are null.

I called an echo with all the variables within that function to see what the heck is going on and of course it prints a blank line. What in the world is out of scope? I have to be missing something simple.

Here's an example of what I'm doing

db_login.php

<?php
    $db_server = 'localhost';
    // ....
?>

functions.php

<?php
    require_once('db_login.php');

    function myfunction() {
        echo "$db_server";
        // ...
    }
?>

Call me crazy, but shouldn't this be simple enough to work?

like image 244
Jeff LaFay Avatar asked Sep 01 '10 04:09

Jeff LaFay


People also ask

What is the use of require_once in PHP?

The require_once keyword is used to embed PHP code from another file. If the file is not found, a fatal error is thrown and the program stops. If the file was already included previously, this statement will not include it again.

What is the difference between include () include_once () and require_once ()?

Require_once means it will need it but only requires it once. Include means it will include a file but it doesn't need it to continue. There is also an include_once function which includes a file once.

What is the difference between require and require_once in PHP?

The require() function is used to include a PHP file into another irrespective of whether the file is included before or not. The require_once() will first check whether a file is already included or not and if it is already included then it will not include it again.

Which PHP statements works identically to the Include except an error is generated and the page stops processing if the referenced file does not exist or is not available?

require ¶ require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning ( E_WARNING ) which allows the script to continue. See the include documentation for how this works.


3 Answers

The variables you declare in db_login.php are globals. In order to access them in your function, you need to use the $GLOBALS variable, e.g. $GLOBALS['db_server'], or declare them as global inside your function using the global keyword, e.g. global $db_server.

like image 40
wuputah Avatar answered Oct 11 '22 03:10

wuputah


PHP doesn't have function scope like Javascript, so you do not have access to the variables in db_login.php inside the functions of functions.php.

There are multiple ways of dealing with this. Due to your probable use of the server name global constants would probably be a good solution, since nothing can change them.

In your case you can do:

<?php
    require_once('db_login.php');
      // You have access to $db_server here.
      // Create a constant.
    define("DB_SERVER", $db_server);

    function myfunction() {
          // Using a constant. Note that there is no "$".
        echo DB_SERVER ;
          // Constants are interpreted inside double quotes too
        echo "\nMy constant is DB_SERVER";
        // ...
    }
?>

In your case having the server name as a constant is probably appropriate. If you are dealing with something that you want to treat as a true variable, you can pass the variable into the function by value or by reference:

myfunction($variable);

  // by value
function myfunction($pass_variable_to_me_by_value)
{
    echo $pass_variable_to_me_by_value;
    // ...
}

function myfunction(& $pass_variable_to_me_by_reference)
{
    echo $pass_variable_to_me_by_reference;
    // ...
}

As a note, in your case, using the global keyword or the $GLOBALS array inside a function is essentially the same as passing by reference., but if you are not passing from the global scope they can be very different (in a Class or from another function for example).

like image 144
Peter Ajtai Avatar answered Oct 11 '22 03:10

Peter Ajtai


Inside of the function "myfunction" you don't have access to these variable...

See more in: http://php.net/manual/en/language.variables.scope.php

like image 20
Felipe Cardoso Martins Avatar answered Oct 11 '22 02:10

Felipe Cardoso Martins