可捕获的致命错误:类PDOStatement的对象无法在第114行转换为字符串

Eer*_*ame 4 php pdo

我正在尝试向我的数据库添加一些数据,但是我收到错误Catchable致命错误:类PDOStatement的对象无法在第114行的/var/www/mandje.php中转换为字符串.这是代码我正在使用:

 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);
Run Code Online (Sandbox Code Playgroud)

我真的不知道这里出了什么问题.任何帮助将非常感激 :)

Roc*_*mat 7

在评论中,您将显示以下内容:

$query = $db->query('SELECT MAX( Bestelnummer ) FROM Bestellingsdetail');
$query->execute();
$max = $query;
$max++;
Run Code Online (Sandbox Code Playgroud)

这不是您从查询中获得结果的方式.要设置$maxPDOStatement对象.您需要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++;
Run Code Online (Sandbox Code Playgroud)

文档:http://www.php.net/pdo.query

$query->execute();准备好的陈述只需要PS . query()将立即执行查询.