Re: Discussion of method -- config files

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



    My preference for years has been like so:

<?php
// inc/config.php
#
# Handles basic configuration settings.
#


// DATABASE SETTINGS

// The name of the database.
$_DB['name'] = "some_database";

// The user with access to the above database.
$_DB['user'] = "some_user";

// The password for the above user.
$_DB['pass'] = "p455w0rD";

// The hostname/IP of the database server.
$_DB['host'] = "localhost";


// GENERAL SITE SETTINGS
// I'd place these in the database, but this is
// for example only here.

// The title of the website.
$_SS['title'] = "Welcome to PHP-General!";


// MAIL SETTINGS
// This can be useful in a configuration
// file to email alerts of DB connect failure.

// The site administrator's email address.
$_MAIL['admin'] = "parasane@xxxxxxxxx";

?>


    Then, in a general file, I include all files that will need to be
included across all pages like so:

<?php
// inc/include_files.php
#
# A generic include file.
#


// The base directory of the website.
$base = dirname(dirname(__FILE__));

// DO NOT USE include_path FOR
// THESE, BECAUSE MULTIPLE
// FILES MAY HAVE THE SAME NAME!

// Basic includes.
include_once($base.'/inc/config.php');
include_once($base.'/inc/db.php');
include_once($base.'/inc/sessions.php');

// Function files.
include_once($base.'/func/db.php');

?>


    And without writing out all files, as I'm sure your imagination
should suffice, I'll show the database basics.  First, in an include
file, define the database connections.  For now, I'll show only a
single-database configuration, but you can take the hint on how
multiple databases work with this.

<?php
// inc/db.php
#
# Define the database connections for as many databases as we need.
#


// Establish the connection with the primary database....
$_DB['conn'] = mysql_connect($_DB['host'],$_DB['user'],$_DB['pass'])
or die ('I couldn\'t connect to the database.  Know why?  Because
MySQL is complaining about something.  Does this make sense to you...?
:<BR />'.mysql_error()."\n");
?>


    Then, in func/db.php, I might have a function like so:

<?php
// func/db.php
#
# Handle multiple connection routines.
#


// This is what we'll use to process and output (or otherwise handle) errors.
function db_error($function) {
        $err  = "MySQL error reached when calling function
".$function."().  MySQL said:<BR />\n";
        $err .= "<FONT COLOR=#FF0000>".mysql_error()."</FONT><BR />\n";
        $err .= "<BR />\n";
        return $err;
}

// Primary database connection.
function site_query($sql) { // Simply return the connection resource ID
        // Since we're in a function, we don't automatically have
access to $_DB.
        global $_DB; // This globalizes the $_DB array for use in this function.
        // Select the site database....
        $site_db = mysql_select_db($_DB['name'],$_DB['conn']);
        $r = mysql_query($sql,$_DB['conn']) or db_error(__FUNCTION__);
        return $r;
}
?>


    Then, since I'm using index.php as my switch page, the beginning
would be something like this:

<?php
// index.php
#
# Where the magic happens!
#


// Generic include.
include('inc/include_files.php');

// Include the header, which will read $_SS['title'] without a problem.
// To be safe, .tpl's are explicitly unreadable by .htaccess config.
include('templates/header.tpl');

// Look up some fake info from our fake database.
$sql = "SELECT DISTINCT something FROM some_table ORDER BY id ASC LIMIT 0,1";
$result = site_query($sql);
$row = mysql_fetch_assoc($result);

echo $row['something']."<br />\n";

?>

    Ignore any errors in here.  I don't claim this code will run
as-is, because I'm "programming" it into this message, so it should be
considered pseudocode only.  This is just an example of how I prefer
to do things.

-- 
</Daniel P. Brown>
Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux