PHP PDO::bindParam() 数据类型..它是如何工作的?

2021-12-25 00:00:00 types php pdo prepared-statement

我想知道 bindParam() 中数据类型的声明是什么(或bindValue()) 用于...

I'm wondering what the declaration of the data type in bindParam() (or bindValue()) is used for...

我的意思是,我认为如果我定义一个整数参数 (PDO::PARAM_INT),该参数必须转换为一个整数,例如

I mean, I thought that if I define an integer argument (PDO::PARAM_INT), the argument must be converted to an integer, something like

$delete->bindParam(1, $kill, PDO::PARAM_INT);
// should work like
$delete->bindParam(1, (int)$kill);

或者至少在参数不是声明的类型时抛出错误.但事实并非如此.

or at least throw an error if the argument is not of the declared type. But this is not the case.

谷歌搜索,我发现在 php.net 档案中:

Googling around, I found that in the php.net archive:

大家好,

我目前正在研究 PDO.确切地在 bindParam() 函数上.第三参数 data_type 似乎在这里强制值的类型?但当我尝试时:

I am currently working on PDO. Exactly on the bindParam() function. The third parameter data_type seems to be here to force the type of the value ? But when I try :

$sql = "INSERT INTO produit (idproduit, nom, marque) VALUES (NULL, :nom, :marque)";
$stmt = $dbh->prepare($sql);
$nom = 'Testarossa'; $marque = 'Ferrari' ;
$stmt->BindValue(':marque',$marque) ;
$stmt->BindParam(':nom',$nom,PDO::PARAM_INT) ;

$stmt->execute(); $nom = '250 GTO' ;
$stmt->execute(); ?>

我希望有一个 PHP我的数据库中的错误或整数.但在我的数据库中,我有:

I was expecting to have either a PHP error or an interger in my database. But in my DB I have :

22 Testarossa 法拉利 23 250 GTO法拉利

22 Testarossa Ferrari 23 250 GTO Ferrari

这意味着如果我没有改变有没有第三个参数.或者也许我错过了什么.有人可以吗对我多一点?或者只是有人可以告诉我在哪里可以找到信息关于它.

It mean that it didn't change if I have the third parameter or not. Or perhaps I miss something. Can someone tole me more ? Or just can someone told me where I can find information about it.

问候,

赛勒斯

这正是我的情况.我的想法哪里出了问题?

That is exactly my situation. Where are my thoughts going wrong?

推荐答案

在其他语言的其他数据库抽象框架中,它可用于确保您对内联值进行正确的转义(对于驱动程序不支持正确的绑定参数)并通过确保数字被适当地二进制打包来提高网络效率(给定协议支持).它看起来像在 PDO 中,它没有做太多事情.

In other DB abstraction frameworks in other languages it can be used for things like making sure you're doing the proper escaping for in-lining values (for drivers that don't support proper bound parameters) and improving network efficiency by making sure numbers are binary packed appropriately (given protocol support). It looks like in PDO, it doesn't do much.

   if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_STR && param->max_value_len <= 0 && ! ZVAL_IS_NULL(param->parameter)) {
                if (Z_TYPE_P(param->parameter) == IS_DOUBLE) {
                        char *p;
                        int len = spprintf(&p, 0, "%F", Z_DVAL_P(param->parameter));
                        ZVAL_STRINGL(param->parameter, p, len, 0);
                } else {
                        convert_to_string(param->parameter);
                }
        } else if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_INT && Z_TYPE_P(param->parameter) == IS_BOOL) {
                convert_to_long(param->parameter);
        } else if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL && Z_TYPE_P(param->parameter) == IS_LONG) {
                convert_to_boolean(param->parameter);
        }

所以,如果你说它是一个 STR(或者你什么都不说,因为这是默认的)并且你的数据的内部类型是一个 double 那么它会使用一种方法将它变成一个字符串,如果它不是一个double 然后它将使用不同的方法将其转换为字符串.

So, if you say it is a STR (or if you say nothing at all as that is the default) and your data's internal type is a double then it will turn it into a string using one method, if it's not a double then it will convert it to a string using a different method.

如果你说它是一个 int 但它实际上是一个 bool 那么它会将它转换为 long.

If you say it's an int but it is really a bool then it will convert it to a long.

如果你说它是一个布尔值,但它实际上是一个数字,那么它会将其转换为真正的布尔值.

If you say it's a bool but it's really a number then it will convert it to a true boolean.

这就是我看到的(快速)查看 stmt 源代码的全部内容,我想一旦您将参数传递给驱动程序,它们就可以发挥额外的作用.所以,我猜你得到的只是一点点做对,以及司机之间的大量行为模糊和差异.

This is really all I saw (quickly) looking at the stmt source, I imagine once you pass the parameters into the driver they can do additional magic. So, I'd guess that all you get is a little bit of do the right and a whole lot of behavior ambiguity and variance between drivers.

相关文章