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 .

Hossein
Posts: 11
Comment
One more i want to s
Reply #9 on : Tue October 01, 2013, 08:04:01
One more i want to say is, people will give their conemmt if they are understand what they read. In my opinion, people who read without understand seems won’t give their conemmt and just type ‘nice post’ or ‘great job’**
Laudilene
Posts: 11
Comment
SUhDOxZVN
Reply #8 on : Wed October 02, 2013, 05:38:21
mingfong ??????????????????????twbbs????????dsato.twbbs.org appears to be a<a href="http://jbyhib.com"> somdubain</a>, please add it as a<a href="http://jbyhib.com"> somdubain</a> to your account.?????subdomain?????????Illegal characters in the naming field, please verify input.------------????????????????????????? ???..
Marlina
Posts: 11
Comment
4p7oWY51Upmu
Reply #7 on : Thu October 03, 2013, 08:32:52
Barney, this is brilliant, and yet so smiple! The DNS has always been the one thing that I felt I had virtually no control over when cutting over a server. This nice little trick gives you the power to make an *instant* controlled cut-over. I can't wait to cut a site over to a new server now ;-)
Arturo
Posts: 11
Comment
Kg29l5bqcG
Reply #6 on : Sat October 05, 2013, 03:27:40
Saw your website in a Tesco free Magazine and since <a href="http://gtykxkau.com">havnig</a> a look i dont know where to start, fabulous for Gifts for everyone. May have to comission a name plaque for each of my girls, not currently on the Secret List as quite unusual.Fabulous, love your work.
Raphael
Posts: 11
Comment
9hLIyCxqIX
Reply #5 on : Sun October 06, 2013, 13:37:39
Hi Jim, thanks for <a href="http://adcyyvxw.com">letnitg</a> me know about this. The WordPress module seems to work fine with Safari, but the Joomla module doesn't. I'm going to look into it and will be back as soon as possible with the solution.
Cory Stiehl
Posts: 11
Comment
Niche345
Reply #4 on : Mon May 11, 2015, 13:40:43
Of course, I understand a little about this post but will try cope with it!!...
[url=http://loanemu.com/]Cory Stiehl[/url]
Insurancewhisper.com
Posts: 11
Comment
Humphrey156
Reply #3 on : Sat May 23, 2015, 04:36:55
I like what you guys are up too. Such smart work and reporting! Carry on the superb works guys I have incorporated you guys to my blogroll. I think it will improve the value of my site . Live among men as if God beheld you speak to God as if men were listening. by Seneca.
[url=http://insurancewhisper.com/how-to-keep-your-insurance-when-relocating.html]Insurancewhisper.com[/url]
Chubby
Posts: 11
Comment
That's a sensible an
Reply #2 on : Tue May 10, 2016, 09:26:42
That's a sensible answer to a chinlengalg question
Twiggy
Posts: 11
Comment
Hi, i feel that i no
Reply #1 on : Sat May 14, 2016, 03:25:46
Hi, i feel that i noticed you visited my web site thus i came to “return the <a href="http://hdhmsec.com">de.sâer€iI</a> am trying to to find things to improve my website!I guess its ok to make use of some of your concepts!!

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.