PHP PDO vs normal mysql_connect

前端 未结 12 916
盖世英雄少女心
盖世英雄少女心 2020-11-27 12:15

Should I use php PDO or normal mysql_connect to execute database queries in PHP?

Which one is faster?

One of the big benefits of PDO is that the interface is

相关标签:
12条回答
  • 2020-11-27 12:31

    I don't think speed is what people are looking for when they are using PDO -- I don't know if there is a difference, and I honnestly don't care : as long as I'm doing a couple of queries to a database when generating a page, a couple of milliseconds on the PHP side will not change anything.

    There are two/three great things with PDO, compared to mysql_* :

    • More or less constant interface accross databases ; better than using mysql_*, pg_*, oci_*, ...
    • Object-Oriented API (mysqli_* has an OO-API, but not mysql_*)
    • Support new features of MySQL >= 4.1 (same as mysqli_*, but not mysql_*, again)

    BTW : I'm generally using PDO -- either "by hand", or as it's integrated in / used by Zend Framework and/or Doctrine.


    As a sidenote : Even if you are not going to use PDO, note that using mysqli instead of mysql is recommended.

    See this page of the PHP manual, about that.

    0 讨论(0)
  • 2020-11-27 12:32

    Some quick timings indicate PDO is slightly faster at connecting.

    $start = microtime(true);
    for($i=0; $i<10000; ++$i) {
    
        try {
            $db = new PDO($dsn, $user, $password);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage()."\n";
        }
        $db = null;
    }
    
    $pdotime = microtime(true) - $start;
    echo "PDO time: ".$pdotime."\n";
    
    $start = microtime(true);
    for($i=0; $i<10000; ++$i) {
        $db = mysql_connect($host, $user, $password);
        if(!$db) {
            echo "Connection failed\n";
        }
        if(!mysql_select_db($schema, $db)) {
            echo "Error: ".mysql_error()."\n";
        }
        mysql_close($db);
    }
    
    $rawtime = microtime(true) - $start;
    echo "Raw time: ".$rawtime."\n";
    

    Gives results like

    PDO time: 0.77983117103577
    Raw time: 0.8918719291687
    
    PDO time: 0.7866849899292
    Raw time: 0.8954758644104
    
    PDO time: 0.77420806884766
    Raw time: 0.90708494186401
    
    PDO time: 0.77484893798828
    Raw time: 0.90069103240967
    

    The speed difference will be negligible anyway; establishing a network connection will likely take a LOT longer than any overhead incurred by PDO, especially if the mysql server is on another host.

    You mentioned all the reasons to use PDO yourself. Really, never use the mysql_* functions directly, either use PDO, or use some other library.

    0 讨论(0)
  • 2020-11-27 12:32

    I would generally recommend using PDO unless there is a specific reason you cannot. If there is no little difference between the two and you have no reason not to use PDO, I believe it would be better to get into the practice of using DB abstraction in your applications than going with mysql_* simply because it is there. I would say let best practice win.

    0 讨论(0)
  • 2020-11-27 12:36

    PDO database connection code:

    <?php
    $dbhost = 'localhost';
    $dbname = 'clsrepair';
    $dbuser = 'root';
    $dbpass = '';
    
    try {
        $db = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    
    catch(PDOException $e) {
        echo "Connection error: ".$e->getMessage();
    }
    ?>
    

    Normal MySQL database connection code:

    <?php 
    mysql_connect("localhost","root", "");
    mysql_select_db ("clsrepair");
    ?>
    

    or

     <?php
    $dbHost = 'localhost'; // usually localhost
    $dbUsername = 'root';
    $dbPassword = '';
    $dbDatabase = 'clsrepair';
    $db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
    mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
    ?>
    

    MySQL database connection code easy but PDO has many advantage.

    0 讨论(0)
  • 2020-11-27 12:41

    Some Advantages of PDO:

    1. Can Access multiple database.
    2. Provided many database driver to connect with different different database.
    3. When you switch from one database to another database, you need not to write all the code to connect with new database, just change the connection string and some query which are required for new database.
    4. It provides prepare statement which is a kind of template of query which compiled only once and can be executed as many times as you want, by just changing the attributes which is called place-holder.
    5. Easy and efficient General Operation like- Insert, update...etc.
    0 讨论(0)
  • 2020-11-27 12:42
    • With PDO you can uses binded params and that will prevent most sql injection attacks.
    • You can gain more speed using PDO prepared statements.
    • standard interface to all db backends
    • There are a bunch of useful methods (like the fetch* family)
    0 讨论(0)
提交回复
热议问题