It is currently 06 Sep 2010, 17:49

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: MySQL driver
PostPosted: 02 Feb 2008, 09:59 

Joined: 29 Jan 2008, 19:28
Posts: 8
Connecting to MySQL
In order to connect to MySQL you can use the following code
Code:
function db_connect($dsn, $username = '', $password = '', $driver_options = array(), $path = './phppdo')
{
    $driver = strtolower(trim(substr($dsn, 0, strpos($dsn, ':'))));
   
    if($driver && class_exists('PDO') && extension_loaded('pdo_' . $driver))
    {
        $class = 'PDO';
    }
    else
    {
        require_once($path . '/phppdo.php');
        $class = 'PHPPDO';
    }
   
    return new $class($dsn, $username, $password, $driver_options);
}

try
{
    $db = db_connect('mysql:dbname=test', 'root', '', array(), '/path/to/phppdo/dir');
} catch(PDOException $e)
{
    die($e->getMessage());
}

Which PHP extension does the driver use by default?
mysqli extension is used by default to connect to the database. If it's not available, the driver tries to use the mysql extension. It is possible to manually choose the extension by appending "extension=mysql" string in your dsn. For example
Code:
    $db = db_connect('mysql:dbname=test;extension=mysql', 'root', '', array(), '/path/to/phppdo/dir');

More info
For more info you can check the PDO documentation at http://www.php.net/pdo
and the driver specific info at http://www.php.net/manual/en/ref.pdo-mysql.php

What is NOT supported
For the mysql extension
  • PDO::MYSQL_ATTR_READ_DEFAULT_FILE
  • PDO::MYSQL_ATTR_READ_DEFAULT_GROUP
  • PDO::MYSQL_ATTR_MAX_BUFFER_SIZE
  • PDO::MYSQL_ATTR_DIRECT_QUERY

For the mysqli extension
  • PDO::MYSQL_ATTR_MAX_BUFFER_SIZE
  • PDO::MYSQL_ATTR_DIRECT_QUERY


Offline
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


 Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
 
cron