php - 可捕获的 fatal error : Object of class PDOStatement could not be converted to string in line 114

标签 php pdo

<分区>

我正在尝试向我的数据库中添加一些数据,但我在第 114 行的/var/www/mandje.php 中收到错误 Catchable fatal error: Object of class PDOStatement could not be converted to string in/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);

我真的不明白这里出了什么问题。任何帮助将不胜感激:)

最佳答案

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

$query = $db->query('SELECT MAX( Bestelnummer ) FROM Bestellingsdetail');
$query->execute();
$max = $query;
$max++;

这不是您从查询中获取结果 的方式。您正在将 $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++;

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

附言$query->execute(); 只有准备好的语句需要。 query() 将立即执行查询。

关于php - 可捕获的 fatal error : Object of class PDOStatement could not be converted to string in line 114,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21464158/

相关文章:

php - 简单的 PDO 查询返回内存大小错误

php - 无法将 Jetstream 包安装到 Laravel 项目

php - 哪些因素使 PHP 与 Unicode 不兼容?

php - 使用 PDO 的 MySQL 全文搜索不起作用

php mysql where 子句

php - 为什么 PDO 将空白值视为空值?

php - 尝试连接/查询 Microsoft Azure SQL 数据库时,DBLIB PDO 驱动程序一直失败

php - 使用 PHP 从网站或文本文档中提取数据并保存在数据库中

Java:同步套接字输入

javascript - php .session_start 显示来自submit.php 的contact.php 上的错误