PHP PDO vs normal mysql_connect

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

提交回复
热议问题