問題描述
如何使用不同的整數(shù)值正確地重新執(zhí)行準(zhǔn)備好的語句?
How do I properly re-execute a prepared statement using different integer values?
重用 ODBC 準(zhǔn)備好的語句時,顯式和隱式綁定 PDO::PARAM_INT
存在致命錯誤.
There's something deathly wrong with explicit and implicit binding PDO::PARAM_INT
when reusing an ODBC prepared statement.
CREATE TABLE mytab (
col INT,
something VARCHAR(20)
);
作品:多個字符串
$pdoDB = new PDO('odbc:Driver=ODBC Driver 13 for SQL Server;
Server='.DATABASE_SERVER.';
Database='.DATABASE_NAME,
DATABASE_USERNAME,
DATABASE_PASSWORD
);
$pdoDB->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$values = ['here','are','some','values'];
$sql = "INSERT INTO mytab (something) VALUES (:something)";
$stmt = $pdoDB->prepare($sql);
foreach ($values as $value)
$stmt->execute(['something'=>$value]);
作品:單個整數(shù)
$values = [42];
$sql = "INSERT INTO mytab (col) VALUES (:col)";
$stmt = $pdoDB->prepare($sql);
foreach ($values as $value)
$stmt->execute(['col'=>$value]);
不起作用:多個整數(shù)
$values = [1,3,5,7,11];
$sql = "INSERT INTO mytab (col) VALUES (:col)";
$stmt = $pdoDB->prepare($sql);
foreach ($values as $value)
$stmt->execute(['col'=>$value]);
它實(shí)際上成功地插入了第一條記錄1
,但是當(dāng)它嘗試在下一次執(zhí)行時重用該語句時失敗了.
It actually successfully inserts the first record 1
but fails when it tries to reuse the statement on the next execute.
PHP 致命錯誤:未捕獲的 PDOException:SQLSTATE[22018]:轉(zhuǎn)換規(guī)范的字符值無效:206 [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]操作數(shù)類型沖突:文本與 int 不兼容(SQLExecute[206] 在/build/php7.0-lPMnpS/php7.0-7.0.8/ext/pdo_odbc/odbc_stmt.c:260)
PHP Fatal error: Uncaught PDOException: SQLSTATE[22018]: Invalid character value for cast specification: 206 [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Operand type clash: text is incompatible with int (SQLExecute[206] at /build/php7.0-lPMnpS/php7.0-7.0.8/ext/pdo_odbc/odbc_stmt.c:260)
我使用 適用于 SQL Server? 的 Microsoft? ODBC 驅(qū)動程序 13(預(yù)覽版)
我已經(jīng)嘗試將整個事情包裝在 PDO::beginTransaction代碼> 和
PDO::commit
一個>
I have tried wrapping the whole thing in PDO::beginTransaction
and PDO::commit
我也試過使用 PDOStatement::bindParam
但它拋出完全相同的錯誤.
I've also tried using PDOStatement::bindParam
but it throws the exact same error.
$values = [1];
$sql = "INSERT INTO mytab (col) VALUES (:col)";
$stmt = $pdoDB->prepare($sql);
foreach ($values as $value){
$stmt->bindParam('col', $value, PDO::PARAM_INT);
$stmt->execute();
}
不起作用
$values = [1,2];
$sql = "INSERT INTO mytab (col) VALUES (:col)";
$stmt = $pdoDB->prepare($sql);
foreach ($values as $value){
$stmt->bindParam('col', $value, PDO::PARAM_INT);
$stmt->execute();
}
我認(rèn)為有趣的是,我在使用 PHP 5.6.9 時遇到了與此未回答的問題完全相同的錯誤.但是,他們甚至無法執(zhí)行一個語句,所以我想知道是否有部分補(bǔ)丁考慮到拋出錯誤的確切行已從 odbc_stmt.c:254
到 odbc_stmt.c:260
I think it's interesting to note that I am getting the exact same error as this unanswered question using PHP 5.6.9. However, they are not able to execute even one statement, so I'm wondering if there's been a partial patch considering the exact line throwing the error has moved from odbc_stmt.c:254
to odbc_stmt.c:260
如果我在循環(huán)中內(nèi)部準(zhǔn)備語句,那么它工作得很好.但我讀到這是非常低效的,我應(yīng)該能夠重用陳述.我特別擔(dān)心在大量數(shù)據(jù)集上使用它.這個可以嗎?有什么我可以做的更好的事情嗎?
If I prepare the statement inside the loop, then it works just fine. But I've read that this is very inefficient and I should be able to reuse the statement. I'm particularly worried about using this with massive datasets. Is this OK? Is there something better that I can do?
$values = [1,3,5,7,9,11];
$sql = "INSERT INTO mytab (col) VALUES (:col)";
foreach ($values as $value){
$stmt = $pdoDB->prepare($sql);
$stmt->execute(['col'=>$value]);
}
推薦答案
在準(zhǔn)備好的語句的情況下,您通常必須在循環(huán)外使用 bindParam
.
In case of prepared statements you have to use bindParam
outside of loop, usually.
bindParam
是單步- 設(shè)置綁定變量是一個可重復(fù)的步驟(循環(huán))
- 你必須為每次重復(fù)運(yùn)行
execute
我想,這樣的事情會奏效:
I guess, something like that would work:
$stmt = $pdoDB->prepare("INSERT INTO mytab (col, key) VALUES (:col, :key)");
// bind params (by reference)
$stmt->bindParams(":col", $col, PDO::PARAM_STR); //bind variable $col
$stmt->bindParams(":key", $key, PDO::PARAM_INT); //bind variable $key
$values = ['here','are','some','values'];
foreach ($values as $i => $value) {
$col = $value; //set col
$key = $i; //set key
$stmt->execute();
}
這篇關(guān)于不能使用整數(shù)值多次執(zhí)行準(zhǔn)備好的語句的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!