ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Why no PDO database driver documentation?

June 01, 2012 6:07pm

Subscribe [5]
  • #1 / Jun 01, 2012 6:07pm

    sidley74

    2 posts

    I was in the process of creating my own PDO-using database wrapper when I realized that CodeIgniter has an utterly undocumented PDO extension for the database driver as of sometime in 2011.  There is, however, absolutely no mention of its existence anywhere in the documentation (but sure enough, there the file was in the source code…)

  • #2 / Jun 01, 2012 6:17pm

    CroNiX

    4713 posts

    Not sure why it’s not in the docs, but it is in the changelog for 2.1.

  • #3 / Jun 01, 2012 10:02pm

    Abel A.

    72 posts

    Very true. Though looks like the ppl at CI are not putting much attention to CI :(

  • #4 / Jun 01, 2012 10:10pm

    CroNiX

    4713 posts

    Based on what?  Have you not seen all of the commits since CI 2.1 for the upcoming CI 3?  As of now (and growing daily), it goes back 45 pages to the November 14, 2011 release of CI 2.1.

  • #5 / Jun 02, 2012 5:40pm

    sidley74

    2 posts

    Not sure why it’s not in the docs, but it is in the changelog for 2.1.

    Yup, that’s the only place I’ve found mention of it.  Nothing in the documentation.

    Also, maybe I just missed it, but looking over the file, I didn’t see any support for using PREPARE or binding variables—really the main points of using PDO in the first place.  PDO with only support for QUERY is pretty useless.

  • #6 / Jun 02, 2012 6:49pm

    CroNiX

    4713 posts

    I know they have greatly improved it in the upcoming CI 3, which you can grab on github (still called 2.1 until release).

  • #7 / Jun 12, 2012 7:01am

    Sweden

    6 posts

    Hello!

    I would also like to use PDO with prepare/execute for my database communication.

    I’ve checked the latest on github, and there is no prepare/Execute in their as I could find.

    How can I use PDO with CodeIgniter?

    Thanks /John S

  • #8 / Jun 12, 2012 5:02pm

    Abel A.

    72 posts

    PDO is its own class. All CI does is initialize the pdo class.

  • #9 / Jun 12, 2012 6:00pm

    sidley74

    2 posts

    PDO is its own class. All CI does is initialize the pdo class.

    Then that needs to documented somewhere—anywhere.

    Because that’s not at all how the other database driver classes work.

  • #10 / Jun 12, 2012 6:14pm

    Sweden

    6 posts

    Hello again! Thanks for your replies!

    Actually I dont understand quite how to use PDO with CI, can you give me very basic example and that would be very helpful.

    /John S

  • #11 / Jun 12, 2012 10:11pm

    Abel A.

    72 posts

    DB config:

    $active_group = 'default';
    $active_record = FALSE;
    
    $db['default']['hostname'] = 'mysql:host=localhost';
    $db['default']['username'] = 'db_user';
    $db['default']['password'] = 'password';
    $db['default']['database'] = 'db_table';
    $db['default']['dbdriver'] = 'pdo';
    $db['default']['dbprefix'] = '';
    $db['default']['pconnect'] = FALSE;
    $db['default']['db_debug'] = TRUE;
    $db['default']['cache_on'] = FALSE;
    $db['default']['cachedir'] = '';
    $db['default']['char_set'] = 'utf8';
    $db['default']['dbcollat'] = 'utf8_general_ci';
    $db['default']['swap_pre'] = '';
    $db['default']['autoinit'] = TRUE;
    $db['default']['stricton'] = FALSE;

    Simple select:

    $myuserid = 10;
    
    $sql = "SELECT userid, name, email 
      FROM table_users 
      WHERE userid = :userid
      LIMIT 1";
    $stmt = $this->db->conn_id->prepare($sql);//prepare the query
    $stmt->bindParam(':userid', $myuserid, PDO::PARAM_STR);//assign params
    $stmt->execute();//execute query
    
    $userinfo = $stmt->fetch();
    
    //user the data
    echo $userinfo['name'];
    echo $userinfo['email'];

    Insert:

    $sql = "INSERT INTO table_strikes (striketime,ip_address,userid) 
      VALUES (:striketime,:ip_address,:userid)";
    $stmt = $this->db->conn_id->prepare($sql);
    
    $stmt->bindParam(':striketime', now(), PDO::PARAM_STR);
    $stmt->bindParam(':ip_address', $this->input->ip_address(), PDO::PARAM_STR);
    $stmt->bindParam(':userid', $myuserid, PDO::PARAM_STR);
    $stmt->execute();

    You need to load the database before running any queries:

    $this->load->database();


    Here’s more things you can do with pdo: http://php.net/manual/en/book.pdo.php

    So far I only used fetch() in the first example, but you can visit php.net for more info.

    All CI does is load the PDO class. CI does NOT rewrite the pdo class, it only connects to the database per your db configuration. You use pdo like you would in any php application. Hence why there really isn’t much info on pdo here in CI.

    Hope that helps.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases