I have a PDOStatement with bind parameters like that:
$stmt = $co->prepare("SELECT * FROM Person WHERE age=:age");
$stmt->bindValue(':age','18',PDO::PARAM_STR);
Now I want to get the actual SQL query that gets executed:
SELECT * FROM Person WHERE age=18
I already tried print_r($stmt);
, but it only prints out the same text that I put in the prepare()
method, i.e. the 18
is not inserted in the query.
I also tried this solution $stmt->debugDumpParams();
, but still, it doesn't give the value of the given parameters.
Putting voluntarily an error in the query and printing the error's message is the closest I got to the solution:
$stmt = $co->prepare("ELECT * FROM Person WHERE age=:age"); //I removed the 'S' of 'SELECT'
$stmt->bindValue(':age','18',PDO::PARAM_STR);
try {
$stmt->execute();
} catch (PDOException $e) {
echo $e->getMessage();
}
Standard ouput:
Error!: [...] near 'ELECT * FROM Person WHERE age=18' at line 1
But the problem with this method is that it only prints the first 80 characters of the query. Obviously in my query it works well, but I want to work with longer queries.
Thank you for your help.