Catchable fatal error: Object of class PDOStatement could not be converted to string in line 114

Catchable fatal error: Object of class PDOStatement could not be converted to string in line 114

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Catchable fatal error: Object of class PDOStatement could not be converted to string in line 114

You are encountering a Catchable fatal error because your code attempts to echo or print a PDOStatement object directly, rather than executing it correctly. In your snippet, the error occurs at line 114 where $count = $db->execute($sql); is called. The logic here assumes that calling execute() on an instance of PDO (which is what $db represents) will return a boolean or handle the result immediately. However, you are passing a raw SQL string to it.

The core issue lies in how modern PHP handles database interactions via PDO. When you call $pdo->execute(), it expects either no arguments (if using prepared statements with placeholders) or an array of bound values. It does not accept a raw SQL string as its first argument unless specific legacy behaviors are enabled, which are generally unsafe and deprecated.

Furthermore, your code mixes two distinct approaches: the query() method and the execute() method. You use $db->query() to fetch the product data, but then switch to $db->execute() for the insertion. This inconsistency is confusing and error-prone. More critically, you are constructing SQL strings by directly concatenating variables like $max, $ProductID, and $price into the query text. This opens your application to severe SQL injection vulnerabilities. If a user can manipulate the $_SESSION["cart"] or if $max contains malicious input, they could alter your database structure or exfiltrate sensitive data.

To fix the "object converted to string" error, you need to ensure that you are not trying to output a statement object unintentionally, but more importantly, you must refactor the insertion logic. The standard pattern for inserting data is to create a new PDOStatement using prepare(), bind your values, and then call execute() without passing the SQL string again.

Here is how you should structure the insertion logic securely:

// Prepare the statement
$sql = "INSERT INTO Bestellingsdetail (Bestelnummer, ProductID, Aantal, Prijs) VALUES (:bestelnummer, :productid, :aantal, :prijs)";

$stmt = $db->prepare($sql);

// Bind the values. Note that we are not concatenating strings here.
$stmt->execute([
    ':bestelnummer' => $max,
    ':productid'    => $ProductID,
    ':aantal'       => $value,
    ':prijs'        => $price
]);

By using prepared statements, the database treats the values as data rather than executable code. This eliminates the SQL injection risk and aligns with how PDO is intended to be used.

Additionally, look at your loop logic. You are iterating through the cart, fetching a row, but you never break out of the while loop after processing the first row. If $value contains multiple items for a single ID (which shouldn't happen in a standard relational schema), or if the loop continues unnecessarily, it might lead to unexpected behavior. Ensure that your inner loop processes exactly one record per iteration and breaks immediately after use:

while($row = $query->fetch(PDO::FETCH_ASSOC)) {
    // Process single row
    break; 
}

If you are working within the Laravel ecosystem, consider utilizing the ORM instead of raw PDO calls. Laravel's Eloquent makes these operations declarative and safer by default. For instance, you could define a Bestellingsdetail model and use create() or save() methods, which handle binding and execution internally. While this adds an abstraction layer, it significantly reduces the likelihood of runtime errors related to statement conversion and SQL syntax construction.

Finally, regarding the error message itself: "Object of class PDOStatement could not be converted to string." This specific error usually appears when you accidentally echo a variable that holds a PDOStatement object instead of the result of an execution. For example, if you had written echo $stmt; instead of checking $stmt->execute(), PHP would try to cast the statement object to a string for output, triggering this fatal error. Always verify what your variables hold before attempting to use them in contexts that require string conversion.

Our network