0

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.

JacopoStanchi
  • 421
  • 5
  • 16
  • 1
    The idea that `18` is interpolated into the SQL string then the string is sent to the server is not how it (usually) works, see: https://stackoverflow.com/questions/210564/getting-raw-sql-query-string-from-pdo-prepared-statements/38049697 – Alex K. Mar 01 '18 at 17:31
  • Just change your code like this: $stmt->bindValue(':age','18',PDO::PARAM_INT); – Abdul Rahman Mar 01 '18 at 17:45

0 Answers0