0

As you can probably tell i am novice to this and trying to make life a little easier for my self by creating a config file with all the settings of my site.

i am doing this as i am learning and trying to push my knowledge that little bit harder and repeating the same code over and over again

i have created a file named settings.php and the code i have at current is this

<?php

return array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);

?>

and in my page i have put the following code

<?php
//set connection variables
$host = include('settings.php');
$username = include('settings.php');
$password = include('settings.php');
$db_name = include('settings.php');

//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
    echo "Error: Could not connect to database.";
    exit;
}
?>

I got this code from another question that can be found here

This seams not to work but i cant be sure as it doesn't give a error.

Before i continue with this i am wanting to add other settings which will be stored in the database is this the best way for me to do this?

Community
  • 1
  • 1
Rick Skeels
  • 513
  • 1
  • 11
  • 30
  • 2
    I think that in the question you refer to the settings are put in 1 variable (as an array). The way you are doing is not working because you set the array to all your variables. – putvande Feb 03 '14 at 21:12

3 Answers3

3

Here is how you would use your code ...

<?php
//set connection variables
$config = include('settings.php');

//connect to mysql server
$mysqli = new mysqli($config['host'], $config['username'], $config['password'], $config['db_name']);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
    echo "Error: Could not connect to database.";
    exit;
}
?>
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
  • 1
    Best answer! No global config variables! Because you can have several config files: main, for each environment, etc. – Oleg Feb 03 '14 at 21:18
  • That works great many thanks - mind explaining to me what @Oleg means by No global config variable! – Rick Skeels Feb 03 '14 at 21:20
  • Under that I meant saving config into a global variable `$settings` instead of returning config as an array. – Oleg Feb 03 '14 at 21:30
  • I'm not saying this is wrong, it is just fine. But if $config is defined in global scope, you still end up with a global variable. On the other side, include scope is inherited meaning if settings.php is included inside a more narrow scope like inside a function, the variables defined in settings.php are within that function's scope, not global. – Jonathan Kuhn Feb 03 '14 at 21:36
0

It doesn't give an error because that is all valid code, however it isn't doing what you think. Typically you include a script once at the top of a page and then any variables defined within that script are available in the page doing the including.

includeMe.php

<?php
$settings = array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);

pageThatNeedsSettings.php

<?php
include("includeMe.php");
//now the settings variable is available to be used
echo $settings['host']; //outputs: localhost
Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
  • 1
    The same: globals variables are evil! – Oleg Feb 03 '14 at 21:17
  • @Oleg Instead of downvoting everyone and just spouting "globals are evil", why don't you offer a better solution. There has to be some global way of accessing the variables. At least they are namespaced under an array. – Jonathan Kuhn Feb 03 '14 at 21:20
  • Because @Christopher has already posted correct answer: http://stackoverflow.com/a/21537886/676479 – Oleg Feb 03 '14 at 21:21
  • And what you mean under "namespaced under array"? )) – Oleg Feb 03 '14 at 21:23
  • @Oleg really that there would only be 1 global variable. Namespace is a bad word there. I've been doing too much javascript lately. – Jonathan Kuhn Feb 03 '14 at 21:24
  • You've already have one global variable in 10 lines of code. Just imagine how much will you have in 10k ;-) – Oleg Feb 03 '14 at 21:26
  • @Oleg The included variables will be available in the scope they are included. Even Christopher's answer has a global variable $config that is the return from the include (assuming $config is defined in global scope). You also can't return multiple variables from the included script. – Jonathan Kuhn Feb 03 '14 at 21:30
  • Just look into such frameworks as Yii or Laravel, how do they work with configuration. Face the fact that your answer is not good enough and stop crying for one minus. At least I've described the reason I've done that. – Oleg Feb 03 '14 at 21:33
  • @Oleg I don't really care about the imaginary points. And I'm not saying using the return value from the include is wrong. In fact I hadn't thought about it before and I like it. I'm just saying using an include is perfectly fine and not wrong. – Jonathan Kuhn Feb 03 '14 at 21:38
0

Several ways to do it using your methodology, but here is one:

$settings = include('settings.php');
$username = $settings['username'];
//etc..

However, just forgo the return business and do this:

$settings = array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);

Use $settings from the include:

include('settings.php');
$username = $settings['username'];
//etc..

Or I would just use $settings['username'] where needed instead of assigning it to $username.

$mysqli = new mysqli($settings['host'], $settings['username'], $settings['password'], $settings['db_name']);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • 1
    Oh no, just forgo global variables! -1 – Oleg Feb 03 '14 at 21:16
  • `$settings` will only be global if the file is included in global scope. Also, "using your methodology", but I agree. Post your solution, I assume a static class? – AbraCadaver Feb 03 '14 at 21:19