本文介绍了PHP PDO :: bindParam()数据类型..它如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 bindParam() (或 bindValue() )用于...

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

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

,或者如果参数不是声明的类型,则至少引发错误.但是事实并非如此.

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

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

解决方案

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

   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,然后将使用其他方法将其转换为字符串.

如果您说这是一个整数,但实际上是布尔值,那么它将把它转换为long.

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

这确实是我(快速)查看stmt源代码所见的全部,我想一旦将参数传递到驱动程序中,它们便可以执行其他操作.因此,我想您所获得的只是一点点正确的做,以及驾驶员之间很多行为上的歧义和差异.

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

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.

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

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

解决方案

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);
        }

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.

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.

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.

这篇关于PHP PDO :: bindParam()数据类型..它如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:15