简体   繁体   中英

PHP arrary config file

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?

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;
}
?>

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

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']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM