0

I have this sql statment:

try {
    $sql = $db->prepare( "INSERT INTO myTable (column_a) VALUES (:value_a)" );

    $sql->execute( array( 
            ":value_a"              => $value_a
    ));
        

} catch (PDOException $e) { 
    echo $e->getMessage();
}

In the catch block, I get the sql error message. But I would like to know, which paramaters and which values were send to the database. Is there a solution for?

Trombone0904
  • 4,132
  • 8
  • 51
  • 104
  • You can quite clearly see the parameters in the query. Debugging the values can be as simple as `var_dump($parameterArray)` or use an actual debugger to halt on exceptions – Phil May 17 '22 at 06:43
  • How about https://stackoverflow.com/questions/210564/getting-raw-sql-query-string-from-pdo-prepared-statements – Nigel Ren May 17 '22 at 06:44

1 Answers1

0

Since you're preparing your variables before the try-clause you can just dump them too in the catch.

try {
    $sql = $db->prepare( "INSERT INTO myTable (column_a) VALUES (:value_a)" );
    $sql->execute( array( 
            ":value_a" => $value_a
    ));    

} catch (PDOException $e) { 
    var_dump($e->getMessage(), $value_a);
}

Also, if you are using PHP 5.4 or higher (higher I really hope) you could/should use short syntax for arrays. It's just nicer and easier to read.

$sql->execute([":value_a" => $value_a]); 
Christoffer
  • 7,470
  • 9
  • 39
  • 55