为什么MySQLi库本身不支持命名参数? [英] Why doesn't MySQLi library natively support named parameters?

查看:46
本文介绍了为什么MySQLi库本身不支持命名参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 http://php.net/的正确的MySQLi参数化查询语法手册/en/mysqli.quickstart.prepared-statements.php :

$stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)");
$stmt->bind_param("i", $id);

但是从来没有像这样的东西:

But never something like:

$stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (:id_value)");
$stmt->bind_param("i", "id_value", $id);

在我看来,named parameter替换是要在API级别实现的合理功能.我很惊讶MySQLi仅在库中实现了unnamed parameters.

It appears to me that named parameter substitution is a reasonable feature to be implemented at the API level. I am surprised that MySQLi only implemented unnamed parameters in the library.

有正当的理由吗?看到PDO,DQL和ORM如何在查询中采用命名参数,这对我来说毫无意义.

Is there a valid reason? It doesn't make sense to me, seeing how PDO, DQL, ORM all have adopted named parameters in their queries.

我希望MySQLi开发人员不会遇到我们懒惰和不想做"的情况.我相信一定有一个很好的理由,我正在寻找这个理由,或者寻找一种找到该理由的方法.在MySQLi扩展库中未实现命名参数的原因.

I hope it was not the case of "We were lazy & don't wanna" on the part of MySQLi developers. I believe there must've been a good reason and I am looking for that reason, or a way to seek out that reason. The reason for named parameters not being implemented in MySQLi extensions library.

推荐答案

MYSQLi不支持命名参数,主要有两个原因:

MYSQLi doesn't support named parameters for two main reasons:

  1. 与包装器一起使用是预定的"(我用这个词比较宽松),
  2. 它的对应物PDO确实可以-而且没有必要重新发明轮子
  1. It is "intended" (I use this term loosely) to be used with a wrapper and
  2. It's counterpart, PDO, does - and there is no point re-inventing the wheel

要详细说明第1点:mysqli尽管与PDO相比有很多下降,但可以很容易地与一个好的包装程序进行比较-也就是说,包装程序而不是本身.这是设计使然的唯一原因:

To elaborate on point 1: mysqli, despite its many downfalls when compared to PDO, becomes easily comparable with a good wrapper - that is, named parameters (among others) are supported by the wrapper rather than mysqli itself. This is by design for one sole reason:

  1. Mysqli被设计为快速而灵活的库.
  1. Mysqli is designed to be a fast and flexible library.

如果开发人员将更多功能集成到基础库中,则直观上来看,它变得不那么灵活,并且需要更长的加载/执行时间.

If the developers incorporated many more features into the base library, it becomes, counter intuitively, less flexible and requires longer load/execution times.

mysqlipdo都是随PHP 5(我相信PDO的版本为5.3)一起发布的,因此可用于不同的用途.

Both mysqli and pdo were released with PHP 5 (PDO with version 5.3, I believe) and as such are intended for different uses.

您想要更快的执行时间?在没有包装的情况下使用mysqli.您要命名参数吗?使用PDO或构建mysqli包装器来处理此类问题-但请注意,这会影响您的执行时间.

You want faster execution times? use mysqli without a wrapper. You want named parameters? use PDO or build a mysqli wrapper to handle such - but be warned, this will hinder your execution times.

这篇关于为什么MySQLi库本身不支持命名参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆