PHP PDO 与普通 mysql_connect

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1402017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:25:41  来源:igfitidea点击:

PHP PDO vs normal mysql_connect

phpmysqlpdo

提问by Imrul

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

我应该使用 php PDO 还是普通的 mysql_connect 在 PHP 中执行数据库查询?

Which one is faster?

哪个更快?

One of the big benefits of PDO is that the interface is consistent across multiple databases. There are some cool functions for prepared statements too, which take some of the hassle out of escaping all your query strings. The portability of PDO is greater than mysql_connect.

PDO 的一大好处是接口在多个数据库之间保持一致。对于准备好的语句也有一些很酷的函数,它们可以减少转义所有查询字符串的麻烦。PDO 的可移植性大于 mysql_connect。

So, should I use PDO for those reasons or stick to the traditional mysql_connect?

那么,我应该出于这些原因使用 PDO 还是坚持使用传统的 mysql_connect?

采纳答案by Sadi

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.

PDO 比 mysql_* 慢一点,但它具有很好的可移植性。PDO 提供跨多个数据库的单一接口。这意味着您可以使用多个数据库,而无需为 mysql 使用 mysql_query,为 MS sql 使用 mssql_query 等。只需始终使用 $db->query("INSERT INTO...") 之类的东西。无论您使用什么数据库驱动程序。

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

因此,对于较大或可移植的项目,PDO 更可取。甚至 Zend 框架也使用 PDO。

回答by gnud

Some quick timings indicate PDO is slightly faster at connecting.

一些快速计时表明 PDO 在连接时稍快。

$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.

无论如何,速度差异都可以忽略不计;建立网络连接可能需要比 PDO 产生的任何开销更长的时间,特别是如果 mysql 服务器在另一台主机上。

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

您提到了自己使用 PDO 的所有原因。说真的,永远不要直接使用mysql_ *功能,或者使用PDO,或者使用一些其他的库

回答by Pascal MARTIN

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.

我不认为速度是人们在使用 PDO 时所寻找的——我不知道是否有区别,老实说我不在乎:只要我做几个查询生成页面时的数据库,PHP 端的几毫秒不会改变任何内容。

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

与 PDO 相比,有两/三件很棒的事情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)
  • 跨数据库或多或少的恒定接口;比使用mysql_*, pg_*, oci_*, ...
  • 面向对象的 API mysqli_*有一个 OO-API,但没有mysql_*
  • 支持 MySQL >= 4.1 的新特性(同mysqli_*,但不是mysql_*,再次)

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

顺便说一句:我通常使用 PDO——要么是“手动”,要么是因为它集成在Zend Framework和/或Doctrine中/使用它。


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


作为旁注:即使您不打算使用 PDO,请注意,建议使用 mysqli 而不是 mysql。

See this page of the PHP manual, about that.

关于这一点,请参阅PHP 手册的这个页面

回答by Pascal MARTIN

  • 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)
  • 使用 PDO,您可以使用绑定参数,这将防止大多数 sql 注入攻击。
  • 您可以使用 PDO 准备好的语句获得更高的速度。
  • 所有数据库后端的标准接口
  • 有很多有用的方法(比如 fetch* 系列)

回答by orrd

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).

我做了一些性能测试,使用准备好的语句和常规的直接查询(在 Mysqlnd 和 MyISAM 表上使用 select 语句测试)来比较 Mysqli 函数和 PDO 函数。

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.

我发现 PDO 查询比 Mysqli 稍微慢一点,但只是稍微慢一点。这是有道理的,因为用于此目的的 PDO 主要只是一个调用 Mysqli 函数的包装器。使用 PDO 的优势在于它可以更轻松地迁移到不同的数据库,因为函数名称不是特定于 MySQL 的。

The real performance difference is in whether you use prepared queries. There is a large and significant performance penaltyto 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.

准备查询更快的唯一时间是如果您准备一次查询,然后使用不同的数据值提交数千次。否则,使用 mysqli::query() 或 PDO::query() 总是更快。但重要的是要注意这些函数不会为您转义数据值,因此您需要记住对数据变量使用 mysqli::real_escape_string() 或 PDO::quote()。

回答by Robert DeBoer

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.

我通常会建议使用 PDO,除非有特定原因不能使用。如果两者之间没有什么区别,并且您没有理由不使用 PDO,那么我相信在您的应用程序中使用 DB 抽象比使用 mysql_* 更好,因为它就在那里。我会说让最佳实践获胜。

回答by Thomas Decaux

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

在这两种情况下,你从同一个 PHP 服务器调用同一个 mySQL 服务器......所以你不会注意到很多差异。

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

如果你想要良好的性能,考虑缓存(memcache 或简单的 Php 文件...)并制作一个好的数据库结构(INDEX...)

回答by Jamil Hneini

  • 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
  • PDO 比 SQl 好
  • PDO 和他的 Prepare 语句提供了针对 SQL 注入的最佳安全代码
  • PDO 是面向对象的 ;)
  • PDO 与之前解释的某些数据库引擎兼容
  • MySQLl_* 已弃用,很快将被删除
  • PDO 以更少的代码行提供更多的功能 示例:

    Pdo

    1. 连接
    2. 检查“<”和“>”和“#”(此检查全局使用)
    3. 准备
    4. 执行
    5. 关闭

MySQL_*

MySQL_*

  1. Connect
  2. Add Backslash
  3. Xsafe
  4. Check for "<" And ">" And "#" (This check for global uses)
  5. Query
  6. Close
  1. 连接
  2. 添加反斜杠
  3. 安全
  4. 检查“<”和“>”和“#”(此检查全局使用)
  5. 询问
  6. 关闭

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

两者具有相同的功能,但您比较代码 PDO 更具人类可读性 :) 那么你怎么看?

回答by Czar Pino

The mysql_connectfunction 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.

mysql_connect函数自 PHP 5.5.0 起已弃用,并且与大多数已弃用的功能一样,将被删除。因此,更喜欢使用PDO_MySQL(或其他替代MySQLi)而不是mysql_connect.

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

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

回答by yoda

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.

如果性能对您来说不是“真正的问题”,您应该使用 PDO。性能差别很小,而且 PDO 有一个非常漂亮且可移植的跨数据库接口,如果您需要使用多个数据库驱动程序,它可以为您省去一些麻烦。