問題描述
我想知道 bindParam() 中數(shù)據(jù)類型的聲明是什么
(或bindValue()
) 用于...
I'm wondering what the declaration of the data type in bindParam()
(or bindValue()
) is used for...
我的意思是,我認(rèn)為如果我定義一個整數(shù)參數(shù) (PDO::PARAM_INT
),該參數(shù)必須轉(zhuǎn)換為一個整數(shù),例如
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);
或者至少在參數(shù)不是聲明的類型時拋出錯誤.但事實并非如此.
or at least throw an error if the argument is not of the declared type. But this is not the case.
谷歌搜索,我發(fā)現(xiàn)在 php.net 檔案中:
Googling around, I found that in the php.net archive:
大家好,
我目前正在研究 PDO.確切地在 bindParam() 函數(shù)上.第三參數(shù) data_type 似乎在這里強(qiáng)制值的類型?但當(dāng)我嘗試時:
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我的數(shù)據(jù)庫中的錯誤或整數(shù).但在我的數(shù)據(jù)庫中,我有:
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
這意味著如果我沒有改變有沒有第三個參數(shù).或者也許我錯過了什么.有人可以嗎對我多一點?或者只是有人可以告訴我在哪里可以找到信息關(guān)于它.
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?
推薦答案
在其他語言的其他數(shù)據(jù)庫抽象框架中,它可用于確保您對內(nèi)聯(lián)值進(jìn)行正確的轉(zhuǎn)義(對于驅(qū)動程序不支持正確的綁定參數(shù))并通過確保數(shù)字被適當(dāng)?shù)囟M(jìn)制打包來提高網(wǎng)絡(luò)效率(給定協(xié)議支持).它看起來像在 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(或者你什么都不說,因為這是默認(rèn)的)并且你的數(shù)據(jù)的內(nèi)部類型是一個 double 那么它會使用一種方法將它變成一個字符串,如果它不是一個double 然后它將使用不同的方法將其轉(zhuǎn)換為字符串.
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 那么它會將它轉(zhuǎn)換為 long.
If you say it's an int but it is really a bool then it will convert it to a long.
如果你說它是一個布爾值,但它實際上是一個數(shù)字,那么它會將其轉(zhuǎn)換為真正的布爾值.
If you say it's a bool but it's really a number then it will convert it to a true boolean.
這就是我看到的(快速)查看 stmt 源代碼的全部內(nèi)容,我想一旦您將參數(shù)傳遞給驅(qū)動程序,它們就可以發(fā)揮額外的作用.所以,我猜你得到的只是一點點做對,以及司機(jī)之間的大量行為模糊和差異.
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.
這篇關(guān)于PHP PDO::bindParam() 數(shù)據(jù)類型..它是如何工作的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!