How to Connect to a Database

Even though you don't need a database for some web applications, there are times when you just can't do without it. This tutorial will show you how to use Raxan to connect to a database server.

Before you can establish a connection to a database there are a few thing that you will need to do.

Load correct PDO drivers

The first thing to do is to make sure you have the correct PHP PDO driver loaded. To do this you will need to include the name of the PDO driver inside your php.ini file. For example:

extension=php_mysql.dll   
    or 
extension=php_mysql.so 

There are several drivers available from the PHP website. If you're connecting to Microsft SQL server from a Windows box, then I would recommend that you download the SQL Server drivers for PHP.

Setting up a connection

Once the PDO driver is loaded you're ready to establish a connect ion. From within your application you can call the Raxan::connect() method by passing the dsn, user name, password and (optional) attributes as shown below:

<?php
    protected function _init() {
        $uid = 'user';
        $pwd = 'password';
        $dsn = 'mysql: host=localhost; dbname=orders';
        $db = Raxan::connect($dsn,$uid,$pwd);
        $this->db = $db;
    }
?>

The above will create a connection to the database that can be used to retrieve and update records.

Using a Database configuration file

In most cases you might want to centrally manage your database connections from a single file source. To do this you will need to create a configuration file and specify the dsn, username, etc:

<?php
    // default database
    $config['db.default'] = array(
        'dsn' => 'mysql: host=localhost; dbname=contacts',
        'user' => 'user',
        'password' => 'password',
        'attribs' =>  array()
    )

    // another database
    $config['db.mydatabase'] = array(
        'dsn' => 'mysql: host=localhost; dbname=inventory',
        'user' => 'user',
        'password' => 'password',
        'attribs' =>  array()
    )

?>

Notice that the database configurations are prefixed with 'db.' To use the above configuration you simple call Raxan::connect() as show below:

<?php
    $db = Raxan::connect('default');     // connect to default database
    $db2 = Raxan::connect('mydatabase');    // connect to another database
?>

For more information please see User Guide - Database connection .

Louise
Posts: 5
Comment
FXWpdkXxUD
Reply #5 on : Tue September 13, 2011, 14:52:18
I'm impressed! You've managed the amlsot impossible.
nifcxm
Posts: 5
Comment
bwUEHDpqo
Reply #4 on : Thu September 15, 2011, 06:46:59
fDKZio , [url=http://zwytctbkryem.com/]zwytctbkryem[/url], [link=http://mtnjmjoaapef.com/]mtnjmjoaapef[/link], http://ypzlevleisqw.com/
zeeyeqehqv
Posts: 5
Comment
boZQOeHH
Reply #3 on : Fri September 16, 2011, 04:14:10
4jGI6W <a href="http://mwnmacdyntxr.com/">mwnmacdyntxr</a>
Morey
Posts: 5
Comment
JscbncVCXJuQ
Reply #2 on : Mon January 30, 2012, 09:25:07
Never would have thunk I would find this so idnpisesnable.
hwlukeh
Posts: 5
Comment
zAUaPvQZO
Reply #1 on : Thu February 02, 2012, 11:58:17
DXUR0t <a href="http://ktjprzsdkuob.com/">ktjprzsdkuob</a>

Leave a Comment

Required fields are marked with *.
If you have trouble reading the code, click on the code itself to generate a new random code.