#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,671
    Rep Power
    171

    In a few words what are the major things PDO can do that mysqli can't


    In a few words what are the major things PDO can do that mysqli can't?
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Connect to database systems other than MySQL.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    It has a sane API.

    MySQLi
    PHP Code:
    $stmt $db->prepare('
        SELECT
            foo
            , bar
        FROM
            my_table
        WHERE
            x = ?
            AND y = ?
    '
    );
    $stmt->bind_param('ss'$x$y);
    $stmt->execute();
    $stmt->bind_result($foo$bar);
    while (
    $stmt->fetch()) {
        
    // do something with $foo and $bar

    PDO
    PHP Code:
    $stmt $db->prepare('
        SELECT
            foo
            , bar
        FROM
            my_table
        WHERE
            x = :x
            AND y = :y
    '
    );
    $stmt->execute(array(
        
    ':x' => $x
        
    ':y' => $y
    ));
    foreach (
    $stmt as $row) {
        
    // do something with $row

    Last edited by Jacques1; July 17th, 2013 at 11:40 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    mysqli can be very confusing when it comes to what you can and can't do when mysqlnd is vs isn't installed. And whether you're using buffered vs unbuffered queries. And whether you're using a plain vs prepared statement. And whether you want to get arrays vs feel like fighting with bound variables.
    PDO is much easier to remember, but you should keep in mind it does unbuffered queries by default (it ties up the server longer and won't let you execute a second query until the first is completely read in).

    I was going to post something like this as a comment for mysqli_stmt on php.net but it needs some more research and some cleanup.
    (If you have mysqlnd and are using prepared statements:)

    get_result() and result_metadata() may return the same type of object but their behaviors are very different.

    get_result():
    * Pro: Allows fetching very easily through mysqli_result
    * Con: Does not work if you store_result() beforehand

    result_metadata():
    * Pro: Works on both buffered and non-buffered resultsets
    * Con: Does not allow fetching (methods return NULL)

    Also remember that you can't use bind_result() when fetching with a mysqli_result object. That's specific to mysqli_stmt.

    So if you
    * want buffering: use result_metadata() and mysqli_stmt::fetch()
    * want binding: use result_metadata() and mysqli_stmt::fetch()
    * want arrays: use get_result() and mysqli_result::fetch_*()

    ---

    Prepared, buffered
    - mysqlnd: stmt fetch
    - no mysqlnd: stmt fetch

    Prepared, unbuffered
    - mysqlnd: get_result fetch_array
    - no mysqlnd: stmt fetch

    Raw, buffered
    - mysqlnd: fetch_array
    - no mysqlnd: fetch_array

    Raw, unbuffered
    - mysqlnd: fetch_array
    - no mysqlnd: fetch_array

IMN logo majestic logo threadwatch logo seochat tools logo