php 可捕获的致命错误:无法在第 114 行将类 PDOStatement 的对象转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21464158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Catchable fatal error: Object of class PDOStatement could not be converted to string in line 114
提问by EerlijkeDame
I'm trying to add some data to my database, but I'm getting the error Catchable fatal error: Object of class PDOStatement could not be converted to string in /var/www/mandje.php on line 114. This is the code I'm using:
我正在尝试向我的数据库中添加一些数据,但出现错误 Catchable fatal error: Object of class PDOStatement could not be convert to string in /var/www/mandje.php on line 114. 这是代码我正在使用:
foreach($_SESSION["cart"] as $id => $value){
$query = $db->query('SELECT * FROM Producten WHERE ProductID ="'.$id.'" ');
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$price = $row['Prijs'];
$ProductID = $row['ProductID'];
}
$sql="INSERT INTO Bestellingsdetail( Bestelnummer, ProductID, Aantal, Prijs)
VALUES ($max,$ProductID,$value,$price)"; //<---- line 114
$count = $db->execute($sql);
I don't really get what's going wrong here. Any help would be much appreciated :)
我真的不明白这里出了什么问题。任何帮助将非常感激 :)
回答by Rocket Hazmat
In the comments, you show the following:
在评论中,您显示以下内容:
$query = $db->query('SELECT MAX( Bestelnummer ) FROM Bestellingsdetail');
$query->execute();
$max = $query;
$max++;
This is not how you get the resultfrom a query. You are setting $maxto a PDOStatementobject. You need to fetch()the result in order to use it.
这不是您从查询中获得结果的方式。您正在设置$max一个PDOStatement对象。你需要fetch()结果才能使用它。
// I've added "AS maxval" to make it easier to get the row
$query = $db->query('SELECT MAX(Bestelnummer) AS maxval FROM Bestellingsdetail');
$max_row = $query->fetch(PDO::FETCH_ASSOC);
$max = $max_row['maxval'];
$max++;
Docs: http://www.php.net/pdo.query
文档:http: //www.php.net/pdo.query
P.S. $query->execute();is only needed for prepared statements. query()will execute the query immediately.
$query->execute();只有准备好的语句才需要PS 。 query()将立即执行查询。
回答by Ant
foreach($_SESSION["cart"] as $id => $value)
{
$query = $db->query('SELECT * FROM Producten WHERE ProductID ="'.$id.'" ');
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
$price = $row['Prijs'];
$ProductID = $row['ProductID'];
}
$array = array( $max, $ProductID, $value, $price );
$sql->prepare
("
INSERT INTO Bestellingsdetail (Bestelnummer, ProductID, Aantal, Prijs)
VALUES (?, ?, ?, ?)
")
$sql->execute($array);
}
回答by Liam Sorsby
Try:
尝试:
foreach($_SESSION["cart"] as $id => $value){
$query = $db->query('SELECT * FROM `Producten` WHERE ProductID ="'.$id.'" ');
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$price = $row['Prijs'];
$ProductID = $row['ProductID'];
}
$sql="INSERT INTO `Bestellingsdetail`( `Bestelnummer`, `ProductID`, `Aantal`, `Prij`s)
VALUES ($max,$ProductID,$value,$price)";
$smtp = $db->prepare($sql);
$count = $smtp->execute();
However, try and use the prepared statements as you are defeating the reason of using PDO and could be at risk of injection:
但是,请尝试使用准备好的语句,因为您正在打败使用 PDO 的原因并且可能有注入的风险:
foreach($_SESSION["cart"] as $id => $value){
$query = $db->query('SELECT * FROM `Producten` WHERE ProductID ="'.$id.'" ');
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$price = $row['Prijs'];
$ProductID = $row['ProductID'];
}
$sql="INSERT INTO `Bestellingsdetail`( `Bestelnummer`, `ProductID`, `Aantal`, `Prijs`)
VALUES (:max,:ProductID,:value,:price)";
$stmt = $db->prepare($sql);
$stmt->bindParam(':max', $max);
$stmt->bindParam(':ProductID', $ProductID);
$stmt->bindParam(':value', $value);
$stmt->bindParam(':price', $price);
$count = $smtp->execute();

