Development Information

May 12, 2012

Database Object – What the Configuration values mean

Filed under: Database Class Information,Tutorials — judda @ 9:37 am

As mentioned in my previous post, there are a few configuration settings that are used, this post will define the meaning of each of them.

Hostname="localhost"
Username="testing"
Password="testing"
Engine="mysql"
Database="testing"
ErrorReporting=2
ErrorLog="logfile.log"
LogQueries="true"
QueryMode=0

Hostname: (normally localhost) the IP address / name of the host that you will be connecting to

Username: the username to connect to the database with

Password: the password to connect to the database with

Engine: the database engine to use

ErrorReporting: The error reporting level(s) that are desired from the database object.  This can be any combination of the following constants (however if used in an INI file you will have to manually set the value).

ERRORS_IGNORE = 0 – Ignore any error messages that come up

ERRORS_ECHO = 1 – Echo any error messages to the screen

ERRORS_EXCEPTION = 2 – Throw an exception whenever there is an issue

ERRORS_LOGFILE = 4 – Write the exception to the log file (located in the ErrorLog configuration setting)

You are able to mix and match any of these levels (except for ERRORS_IGNORE).  For example, in the PHP code you could do the following:

$config->errorReporting=DatabaseConfiguration::ERRORS_ECHO | DatabaseConfiguration::ERRORS_EXCEPTION;

And you would get the errors both echoed out to the screen AND an exception would be thrown.

ErrorLog: If error logging to a log file is enabled, then the error messages will be written here (need to have write access to it).

LogQueries: Whether or not you want a query log stored in memory with the object (useful for helping to determine long running queries and various other query stats)

QueryMode: One of two settings which will define the way that the queries get parsed for parameters.

QUERY_DEFAULT = 0 – Use the new style parameters (i.e. {0:uf})

QUERY_CLASSIC = 1 – Use the classic style parameters (i.e. %uf)

With these values in mind, you are able to set up the object and start querying your database.

Configuring the Database Object

Filed under: Database Class Information,Tutorials — judda @ 9:23 am

The first step to using the database object is the configuration of it.  Essentially at the configuration process is just the hydrating of the DatabaseConfiguration object.  There are four relatively easy ways to do this.

Next post I’ll explain what each of these mean, and what values they should contain.

They are as follows:

1 – Using an INI file

DatabaseConfiguration::fromINIFile($iniFile, $section=NULL)

If you store your configuration settings in a .ini file, you are able to use that to configure the object.  The ini file should look as follows:

; Configuration INI File for Database Configuration
[Database]
Hostname='localhost'
Username='testing'
Password='testing'
Engine='mysql'
Database='testing'
ErrorReporting=2
ErrorLog='logfile.log'
LogQueries='true'
QueryMode=0

With this as your ini file you can then call the static method DatabaseConfiguration::fromINIFile passing it the full file path to this ini file and if there is a section (i.e. the ‘Database’ in the ini file seen above) then provide it that name, and then it will automatically configure the rest for you.

2 – Using an INI string

DatabaseConfiguration::fromINIString($iniString, $section=NULL)

You may already have the body of an ini file in memory, so this way works just like the previous method but instead of reading the contents of the ini file from a flat file, it will just load it from a string.

3 – Using an array

DatabaseConfiguration::fromArray($config)

If you have an array which contains the following key values (keys the same as seen in the above example) then you can set up the object using that.

$array = array (
    'Hostname' => 'localhost'
    , 'Username' => 'testing'
    , 'Password' => 'testing'
    , 'Engine' => 'mysql'
    , 'Database' => 'testing'
    , 'ErrorReporting' => 2
    , 'ErrorLog' => 'logfile.log'
    , 'LogQueries' => 'true'
    , 'QueryMode' => 0
);

4 – Manually Hydrating the object

If none of the previous options work for you, then you are always able to manually create an instance of the DatabaseConfiguration object and populate each of the values by hand.

$config=new DatabaseConfiguration();
$config->hostname = 'localhost';
$config->engine = 'mysql';
$config->database = 'testing';
$config->username = 'testing';
$config->password = 'testing';
$config->errorReporting = DatabaseConfiguration::ERRORS_EXCEPTION;
$config->errorLogFile = 'logfile.log';
$config->maintainQueryLog = true;
$config->queryMode = DatabaseConfiguration::QUERY_DEFAULT;

May 6, 2012

Database Class v3.0.0

Filed under: Database Class Information,Updates — judda @ 2:45 pm

The first cut of the new database object has been completed.  From what I’ve found, it is a lot easier to use than the older versions of it.  Not only to configure, but some things have been moved around to make it much more intuitive.  I will be adding more posts on here walking through the basics on how to set up your object, as well as how to query in the new fashion.

Powered by WordPress