MySQL、MySQLi、MySQL 准备语句还是 PDO?

发布于 2024-10-17 07:56:56 字数 452 浏览 3 评论 0原文

在过去的几天里,我一直在关注许多有关与数据库交互的不同方式的教程。像旧的 MySQL 方式、MySQLi、MySQL 准备语句、PHP 的 PDO 等。

我仍然是编程的新手,因为我学习和开始编码还不到一年。我有时会付出很多努力来改进我的代码并遵守网络开发人员定义的标准。现在是我觉得我需要改变使用旧的 MySQL 方式(如 mysql_connect() 和 mysql_query())与数据库交互的方式的时候了。

我的生产机器上有以下配置。

  • 安装了 MAMP 的 Mac OSX 10.6
  • PHP 版本:5.2.13
  • MySQL 客户端版本:5.1.44

您认为我应该使用什么来访问数据库,为什么?

  • MySQL
  • MySQLi
  • MySQL 准备语句
  • PDO

from the past few days i have been following a lot of tutorials regarding different ways to interact with database. like the old MySQL way, MySQLi, MySQL prepared statement, PHP's PDO etc.

i am still a new comer to the programming as it has been hardly 1 year since i learned and started coding. from time to time i have made lot of efforts on improving my codes and adhere to the standards as defined by the web developers. and now is the time i feel i change the way i am used to interact with the database using old MySQL way like mysql_connect() and mysql_query().

I have following configuration on my production machine.

  • Mac OSX 10.6 with MAMP installed
  • PHP version : 5.2.13
  • MySQL client version: 5.1.44

what do you think i should be using for database access and why?

  • MySQL
  • MySQLi
  • MySQL Prepared Statement
  • PDO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够钟 2024-10-24 07:56:56
  • Mysql ext 已经不复存在了。
  • 假设MySQL准备语句是一个SQL实现,我发现它在PHP中几乎不可用,

这使得我们只有两个可能的选择

  • PDO。如果您计划按原样使用数据库 API,那么 PDO 显然是赢家,因为它比 mysqli 对用户友好得多。此外,如果您正在编写开源库,那么 PDO 是唯一的选择,因为它允许用户连接任何支持的数据库。总而言之,PDO 提供了一个很好的经验法则:如果您不知道要使用哪个驱动程序 - 直接使用 PDO。
  • mysqli。如果您打算编写一个数据库包装器来封装原始 API 函数,并且您的数据库后端将始终保留 mysql,并且命名参数对您来说不是一个游戏规则改变者,那么 mysqli 也可能是一个不错的选择,它提供了一些方便的特定于 mysql 的功能诸如 mysqli_info() 之类的函数。
  • Mysql ext is no more.
  • Assuming MySQL Prepared Statement is an SQL implementation, I find it hardly usable in PHP

which leaves us only two possible choices

  • PDO. If you are planning to use the database API as is, then PDO is the obvious winner, being much more user friendly than mysqli. Also, if you are writing an open-source library, then PDO is the only choice as it will let a user to connect any database supported. All in all, PDO makes a good rule of thumb: if you don't know which driver to use - go straight for PDO.
  • mysqli. If you are going to write a database wrapper to encapsulate raw API functions and your database backend will always remain mysql and named parameters is not a game changer for you, then mysqli could be a good choice as well, providing a few handy mysql-specific functions such as mysqli_info().
相思故 2024-10-24 07:56:56

这实际上取决于你在做什么。

如果您正在执行大量都使用准备好的语句的大量查询,准备好的语句(通过 mysqli 或 PDO)可能会很有用。准备好的语句速度更快,此外,您不必担心使用准备好的语句转义数据之类的事情,但它们需要一些代码开销,因此对于一个简单的应用程序,有时不值得使用准备好的语句,并且使用旧的 mysql_* 函数更容易达到预期目的。

It really depends on what you are doing.

Prepared statements (via mysqli or PDO) can be useful if you are doing lots of high volume queries that all use the prepared statement. The prepared statements are faster, additionally you don't have to worry about things like escaping data with prepared statements but they take a bit of code overhead so for a simple app sometimes it's not worth using prepared statements and using the old mysql_* functions are easier for the intended purpose.

2024-10-24 07:56:56

与直接使用 MySQLi 相比,PDO for MySQL 有几个主要优点。

  1. PDO 支持多个数据库后端。如果您的客户之一请求将您的应用程序移植到不同的数据库,这会有所帮助。
  2. 除了 MySQLi 支持的位置 (?) 占位符之外,PDO 中的预准备语句还支持命名占位符。
  3. PDO 支持一次绑定一个参数,而不是一次绑定所有参数。这允许逐步遍历参数数组并绑定每个参数,而不必构造类型字符串,然后使用 call_user_func_array() 黑魔法。
  4. PDO 通过将数组传递给 $stmt->execute() 来支持便捷的快捷方式,其中每个键的值都作为键绑定到占位符。 (警告:它将每个值转换为一个字符串,因此如果您的语句有一个变量 LIMIT 这是记录的。 ,并且有一个功能请求将此转换更改为字符串 .)

将第 2 条到第 4 条结合起来,可以更轻松地以可证明注入安全的方式表达运算符 IN 的右侧。使用 PDO,您可以构建具有顺序名称的关联数组(例如 [':likeval0'=>$val0、':likeval1'=>$val1、':likeval2'=>$val2]) 然后在该数组的右侧安全地构建一个占位符列表。代码可能如下所示:

$args = [];
foreach ($usernames as $n=>$value) {
    $args[":likeval$n"] = $value;
}
$list = implode(',', array_keys($args));
// result is like ':likeval0,:likeval1,:likeval2'
$stmt = "SELECT * FROM app_users WHERE `username` IN ($list)";
$stmt = $dbh->prepare($stmt);
$c = $stmt->execute($args);

MySQLi 仅支持 ? 占位符和通过变量参数函数调用进行一次性绑定。在 MySQLi 中,通过对列表中的每个元素使用 $dbh->escape_string() 来构造运算符 IN 的右侧实际上要容易得多。如果经过充分测试,这种方法对于 SQL 注入是安全的,但会引起一些“Bobby Tables”参数化纯粹主义者。

PDO for MySQL has several major advantages over directly using MySQLi.

  1. PDO supports more than one database backend. This helps should one of your clients request a port of your application to a different database.
  2. Prepared statements in PDO support named placeholders in addition to the positional (?) placeholders that MySQLi supports.
  3. PDO supports binding a single parameter at a time instead of all parameters at once. This allows stepping through an array of arguments and binding each, instead of having to construct a type string and then use call_user_func_array() black magic.
  4. PDO supports a convenient shortcut by passing an array to $stmt->execute(), where each key's value is bound to the placeholder as the key. (Caveat: It casts each value to a string, so you have to bind one at a time if your statement has a variable LIMIT. This is documented, and there's a feature request to change this casting to string.)

Items 2 through 4 combine to make it much easier to express the right side of operator IN in a provably injection-safe manner. With PDO, you can build an associative array with sequential names (such as [':likeval0'=>$val0, ':likeval1'=>$val1, ':likeval2'=>$val2]) and then safely build a placeholder list on the right side out of that array. Code might look like this:

$args = [];
foreach ($usernames as $n=>$value) {
    $args[":likeval$n"] = $value;
}
$list = implode(',', array_keys($args));
// result is like ':likeval0,:likeval1,:likeval2'
$stmt = "SELECT * FROM app_users WHERE `username` IN ($list)";
$stmt = $dbh->prepare($stmt);
$c = $stmt->execute($args);

MySQLi supports only ? placeholders and all-at-once binding through a variable argument function call. In MySQLi, constructing the right side of operator IN is actually a lot easier by using $dbh->escape_string() for each element in a list. This approach is safe against SQL injection if well tested but raises red flags with some "Bobby Tables" parameterization purists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文