Welcome Guest, Not a member yet? Register   Sign In
PDO integration with CI 2.1.0
#1

[eluser]sathish_nec[/eluser]
Hi,

I know there might be an answer for my question already. But since I am not able to find them on my search, please could some one help with my query. I would like to use to PDO driver for my d/b access with CI 2.1.0. From the forum, my understanding is that version 2.1.0 does not support PDO very well and there were many developments made on the github for the PDO driver at the moment. If so, please can you let me know how to integrate the PDO driver from github to CI 2.1.0.
#2

[eluser]ppwalks[/eluser]
Go to phpclasses.org and use one of their many classes then google using external classes with codeigniter, there is a great deal of info about it and you should get it working.

Whats wrong with Cl's native db infurstructure?
#3

[eluser]jvicab[/eluser]
Actualy it is quite easy. try this:

1- Create a file (like pdodb.php) inside application/library folder an copy this code on it:

Code:
class PDOdb
{
    private static $_pdo;
    
    public static function getInstance()
    {
        if (self::$_pdo === null) {
            $CI =& get_instance();
    
            $host = $CI->db->hostname;
            $dbname = $CI->db->database;
            $user = $CI->db->username;
            $password = $CI->db->password;      
            $dsn = 'mysql:dbname='.$dbname.';host='.$host;

            try {
                self::$_pdo = new PDO($dsn, $user, $password);
            } catch (PDOException $e) {
                self::$_pdo =  null;
            }    
        }
        return self::$_pdo;
    }

}

2- Use it as any other library, like:

Code:
$this->load->library('PDOdb'); // or better get it autoloaded in config/autoload.php
$db = PDOdb::getInstance();

// use it, like:
$query = "SELECT * FROM mytable WHERE id = :id";
$q = $db->prepare($query);
$q->bindParam(':id', $_POST['id']);
if ($q->execute()) {
    while ($row = $q->fetch()) {
        ......      
    }
}

Good luck!
#4

[eluser]jvicab[/eluser]
did it help?
#5

[eluser]sathish_nec[/eluser]
@jvicab :Thanks a lot for the reply. I will try this out now.




Theme © iAndrew 2016 - Forum software by © MyBB