問題描述
我目前正在通過切換到 PDO 來更新我的應(yīng)用.我有以下代碼:
I'm currently updating my app by switching to PDO. I have the following code:
$stmt = $db->prepare("select * from `product` where productid in (:productidLst)");
$stmt->bindParam(":productidLst",$productidLst, PDO::PARAM_INT);
$stmt->execute();
在上面的代碼之后,var $productidLst 是 1,2 我想使用 PDO 等價(jià)物:
The var $productidLst is 1,2 after the above code I would like to use the PDO equivalent of this:
while($rs=mysql_fetch_assoc($res)){
$rs['qty']=$_SESSION['basket'][$rs['productid']];
$rs['total'] += $rs['qty']*$rs['price'];
$total += $rs['total'];
$a[] = $rs;
}
我嘗試了多種組合,但都沒有成功,因此將不勝感激(在第二個(gè)代碼塊中,$res 是 sql).其次,我已將參數(shù) $productidLst 設(shè)置為 INT 這是正確的還是應(yīng)該是字符串?
I have tried numerous combinations but not been successful so any help with this would be appreciated (in the 2nd code block $res was the sql). Secondly I have set the Parameter $productidLst to INT is this correct or should it be a string?
------------更新 1---------------------------------------------------
--------------------UPDATE 1----------------------------------------------------
我嘗試了以下代碼:
$stmt = $db->prepare("select * from `product` where productid in (:productidLst)");
foreach ($stmt->execute(array(':productidLst' => $productidLst)) as $row)
{
$total += $row['total'];
}
返回:為 foreach() 錯(cuò)誤提供的無效參數(shù)
Which returns: Invalid argument supplied for foreach() error
推薦答案
PHP 手冊(cè)中的標(biāo)準(zhǔn)文檔通常很有幫助.PHP手冊(cè)中有一個(gè)用PDO執(zhí)行for循環(huán)的例子,PDO詳情.
The standard documentation in the PHP manual is usually pretty helpful. There is an example of executing a for loop with PDO in the PHP manual, PDO Details.
function getFruit($conn) {
$sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
foreach ($conn->query($sql) as $row) {
print $row['name'] . " ";
print $row['color'] . " ";
print $row['calories'] . "
";
}
}
通過一些更改,該示例可以使用準(zhǔn)備好的語句.
With a few changes, the example can be made to use a prepared statement.
function getFruit($conn) {
$query = $conn->prepare('SELECT name, color, calories FROM fruit WHERE kind=:kind ORDER BY name');
$query->execute(array(':kind' => 'drupe'));
// alternatively you could use PDOStatement::fetchAll() and get rid of the loop
// this is dependent upon the design of your app
foreach ($query as $row) {
print $row['name'] . " ";
print $row['color'] . " ";
print $row['calories'] . "
";
}
}
您還可以使用 while
循環(huán)和 PDOStatement::fetch
獲取每一行.
You can also use a while
loop and PDOStatement::fetch
to get each row.
function getFruit($conn) {
$query = $conn->prepare('SELECT name, color, calories FROM fruit WHERE kind=:kind ORDER BY name');
$query->execute(array(':kind' => 'drupe'));
// alternatively you could use PDOStatement::fetchAll() and get rid of the loop
// this is dependent upon the design of your app
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
print $row['name'] . " ";
print $row['color'] . " ";
print $row['calories'] . "
";
}
}
PHP 手冊(cè)在提供創(chuàng)建后兩個(gè)版本所需的所有信息方面仍然非常有用.
The PHP manual remains quite helpful in providing all the necessary information to create the latter two versions.
上一個(gè)版本的解釋:假設(shè) $conn
是一個(gè)有效的 PDO 對(duì)象.$conn->prepare($sql)
返回一個(gè) PDOStatement
對(duì)象如果成功,false
失敗 OR 基于您的錯(cuò)誤處理的異常.因此,假設(shè)成功,我們希望實(shí)際從對(duì)象中獲取數(shù)據(jù).我們可以使用 $query->fetch()
在循環(huán)或 $query->fetchAll()
獲取依賴于您的應(yīng)用的數(shù)據(jù).傳入類常量 PDO::FETCH_ASSOC
將返回一個(gè)數(shù)據(jù)關(guān)聯(lián)數(shù)組.
Explanation of the last version: assuming $conn
is a valid PDO object. $conn->prepare($sql)
returns a PDOStatement
object if successful, false
on failure OR an exception based on your error handling. So, assuming success we would want to actually get the data from the object. We can use $query->fetch()
in a loop or $query->fetchAll()
to get the data dependent upon your app. Passing in the class constant PDO::FETCH_ASSOC
will return, you guessed it, an associative array of data.
在功能上,foreach
和 while
實(shí)現(xiàn)是等效的.從概念上講,foreach
更合適,因?yàn)?while
循環(huán)具有在靜態(tài)條件成立時(shí)循環(huán)的含義,而 foreach
循環(huán)遍歷 a 的元素收藏.閱讀一段時(shí)間之間的差異PHP 中的循環(huán)和 for 循環(huán)?" 部分故事.
Functionally, the foreach
and while
implementations are equivalent. Conceptually, a foreach
is more appropriate, as a while
loop has connotations of looping while a static condition holds, whereas foreach
loops over elements of a collection. Read "Differences between a while loop and a for loop in PHP?" for part of the story.
請(qǐng)務(wù)必閱讀 關(guān)于 PDO 的 php.net 參考
這篇關(guān)于將 PHP while 循環(huán)轉(zhuǎn)換為使用 PDO的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!