PHP PDO vs normal mysql_connect

前端 未结 12 917
盖世英雄少女心
盖世英雄少女心 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:42

    In both cases, you call the same mySQL server from the same Php server ... so you cannot notice a lot of difference.

    If you want good performance, think about cache (memcache or simple Php file ...) and make a good data base structure (INDEX ...)

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

    PDO is a bit slower than the mysql_* But it has great portability. PDO provides single interface across multiple databases. That means you can use multiple DB without using mysql_query for mysql, mssql_query for MS sql etc. Just use something like $db->query("INSERT INTO...") always. No matter what DB driver you are using.

    So, for larger or portable project PDO is preferable. Even zend framework use PDO.

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

    I did some performance testing to compare Mysqli functions to PDO functions using both prepared statements and regular direct queries (tested using select statements on Mysqlnd and MyISAM tables).

    I found that PDO queries are just slightly slower than Mysqli, but only slightly. This makes sense since PDO used for this purpose mostly just a wrapper that calls Mysqli functions. The advantage to using PDO is that it makes it a little easier to migrate to a different database because the function names aren't specific to MySQL.

    The real performance difference is in whether you use prepared queries. There is a large and significant performance penalty to using prepared queries. Other people who have tested them have found the same results.

    The only time prepared queries are faster is if you are preparing a query once and then submitting it thousands of times with different data values. Otherwise, it's always faster to use mysqli::query() or PDO::query(). But it's important to be aware that those functions don't escape data values for you, so you need to remember to use mysqli::real_ escape_ string() or PDO::quote() on data variables.

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

    The mysql_connect function is deprecated as of PHP 5.5.0 and, as with most deprecated features, will be removed. Therefore, prefer using PDO_MySQL (or another alternative MySQLi ) over mysql_connect.

    Source: http://php.net/manual/en/function.mysql-connect.php

    0 讨论(0)
    • PDO is better than SQl
    • PDO And His Prepare Statement Provide Best Secure Code against SQL injection
    • PDO is Object Oriented ;)
    • PDO is compatible with some Databases Engine As Explained Before
    • MySQLl_* Is Deprecated and will be removed soon
    • PDO provide more functionality with less line of codes Example:

      Pdo

      1. Connect
      2. Check for "<" And ">" And "#" (This check for global uses)
      3. Prepare
      4. Execute
      5. Close

    MySQL_*

    1. Connect
    2. Add Backslash
    3. Xsafe
    4. Check for "<" And ">" And "#" (This check for global uses)
    5. Query
    6. Close

    both the same functionality but you compare for codes PDO is more Humanly Readable :) So what you think?

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

    If performance isn't a "real problem" for you, you should use PDO. The performance differs by small margins, and PDO has a very nice and portable cross-database interface wich can save you some headaches in case you need to use several database drivers.

    0 讨论(0)
提交回复
热议问题