問題描述
我目前正在通過切換到 PDO 來更新我的應用.我有以下代碼:
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 等價物:
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;
}
我嘗試了多種組合,但都沒有成功,因此將不勝感激(在第二個代碼塊中,$res 是 sql).其次,我已將參數 $productidLst 設置為 INT 這是正確的還是應該是字符串?
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() 錯誤提供的無效參數
Which returns: Invalid argument supplied for foreach() error
推薦答案
PHP 手冊中的標準文檔通常很有幫助.PHP手冊中有一個用PDO執行for循環的例子,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'] . "
";
}
}
通過一些更改,該示例可以使用準備好的語句.
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
循環和 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 手冊在提供創建后兩個版本所需的所有信息方面仍然非常有用.
The PHP manual remains quite helpful in providing all the necessary information to create the latter two versions.
上一個版本的解釋:假設 $conn
是一個有效的 PDO 對象.$conn->prepare($sql)
返回一個 PDOStatement
對象如果成功,false
失敗 OR 基于您的錯誤處理的異常.因此,假設成功,我們希望實際從對象中獲取數據.我們可以使用 $query->fetch()
在循環或 $query->fetchAll()
獲取依賴于您的應用的數據.傳入類常量 PDO::FETCH_ASSOC
將返回一個數據關聯數組.
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
實現是等效的.從概念上講,foreach
更合適,因為 while
循環具有在靜態條件成立時循環的含義,而 foreach
循環遍歷 a 的元素收藏.閱讀一段時間之間的差異PHP 中的循環和 for 循環?" 部分故事.
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.
請務必閱讀 關于 PDO 的 php.net 參考
這篇關于將 PHP while 循環轉換為使用 PDO的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!