ZetCode

PHP PDOStatement::debugDumpParams

last modified April 19, 2025

The PDOStatement::debugDumpParams method is a debugging tool in PHP's PDO. It dumps the prepared statement and its bound parameters information.

Basic Definition

PDOStatement::debugDumpParams displays the SQL prepared statement. It shows the number of parameters and their types and values.

Syntax: PDOStatement::debugDumpParams(): void. This method doesn't return anything but outputs directly.

Simple debugDumpParams Example

This shows basic usage of debugDumpParams with a simple query.

debug_simple.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
    $stmt->bindValue(1, 5, PDO::PARAM_INT);
    $stmt->debugDumpParams();
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This prepares a statement with one parameter and binds a value to it. debugDumpParams outputs the SQL query and parameter information. The output helps verify the query and parameter binding.

debugDumpParams with Named Parameters

This demonstrates debugDumpParams with named parameters.

debug_named.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt = $pdo->prepare('SELECT * FROM users WHERE name = :name AND status = :status');
    $stmt->bindValue(':name', 'John', PDO::PARAM_STR);
    $stmt->bindValue(':status', 'active', PDO::PARAM_STR);
    $stmt->debugDumpParams();
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This uses named parameters in the prepared statement. debugDumpParams shows the parameter names and their bound values. It's useful for verifying complex queries with multiple parameters.

debugDumpParams Before Binding

This shows debugDumpParams output before binding parameters.

debug_unbound.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt = $pdo->prepare('INSERT INTO products (name, price) VALUES (?, ?)');
    $stmt->debugDumpParams();
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This demonstrates debugDumpParams before any parameters are bound. The output shows the SQL with placeholders but no parameter values. This helps verify the query structure before execution.

debugDumpParams with execute()

This shows debugDumpParams after executing a statement.

debug_executed.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt = $pdo->prepare('UPDATE orders SET status = ? WHERE id = ?');
    $stmt->execute(['shipped', 42]);
    $stmt->debugDumpParams();
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This executes a statement with parameters passed to execute(). debugDumpParams shows the final parameter values used in execution. The output helps verify what values were actually used in the query.

debugDumpParams with Different Parameter Types

This demonstrates debugDumpParams with various parameter types.

debug_types.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt = $pdo->prepare('INSERT INTO data (int_val, str_val, bool_val, null_val) VALUES (?, ?, ?, ?)');
    $stmt->bindValue(1, 123, PDO::PARAM_INT);
    $stmt->bindValue(2, 'text', PDO::PARAM_STR);
    $stmt->bindValue(3, true, PDO::PARAM_BOOL);
    $stmt->bindValue(4, null, PDO::PARAM_NULL);
    $stmt->debugDumpParams();
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This binds parameters of different types (integer, string, boolean, null). debugDumpParams shows each parameter's type and value. This helps verify type handling in prepared statements.

debugDumpParams in Transaction

This shows debugDumpParams usage within a transaction.

debug_transaction.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $pdo->beginTransaction();
    
    $stmt = $pdo->prepare('UPDATE accounts SET balance = balance + ? WHERE user_id = ?');
    $stmt->bindValue(1, 100.50, PDO::PARAM_STR);
    $stmt->bindValue(2, 7, PDO::PARAM_INT);
    $stmt->debugDumpParams();
    
    $pdo->commit();
} catch (PDOException $e) {
    $pdo->rollBack();
    echo "Error: " . $e->getMessage();
}

This uses debugDumpParams within a transaction context. The output helps verify parameter binding before committing changes. It's useful for debugging complex transactional operations.

debugDumpParams with Multiple Statements

This demonstrates debugDumpParams with multiple prepared statements.

debug_multiple.php
<?php

declare(strict_types=1);

try {
    $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt1 = $pdo->prepare('SELECT * FROM users WHERE id = ?');
    $stmt1->bindValue(1, 10, PDO::PARAM_INT);
    $stmt1->debugDumpParams();
    
    $stmt2 = $pdo->prepare('DELETE FROM logs WHERE created_at < ?');
    $stmt2->bindValue(1, '2023-01-01', PDO::PARAM_STR);
    $stmt2->debugDumpParams();
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

This shows debugDumpParams output for two different statements. Each statement's parameters are displayed separately. This helps debug applications with multiple database operations.

Best Practices

Source

PHP debugDumpParams Documentation

This tutorial covered the PDOStatement::debugDumpParams method with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP PDO Functions.