When we are writing our SQL queries, we may need to check how the dynamic data we are sending is behaving, to validate that the result is expected.
EXAMPLE
I have the following script to bring all the posts of a certain user working 2 tables by means of a JOIN
<?php
$conexion = new PDO("mysql:host=localhost;dbname=blog;port=3309", "root", "password");
$id = 1;
$consulta = $conexion->prepare("SELECT nameUser, namePost
FROM users
JOIN posts ON users.id = posts.user_id
WHERE users.id = :id");
$consulta->bindParam(":id", $id, PDO::PARAM_INT);
$consulta->execute();
var_dump($consulta);
However, if I want to dump from console the SQL query that is being assembled by the dynamic data that arrives by $id
we can do the following
var_dump($consulta);
RESULT
object(PDOStatement)#2 (1) {
["queryString"]=>
string(96) "SELECT nameUser, namePost FROM users JOIN posts ON users.id
= posts.user_id WHERE users.id = :id"
}
or you can also use debugDumpParams
that will give you a structure similar to the following
SQL: [82] SELECT nameUser, namePost
FROM users
JOIN posts ON users.id = posts.user_id
WHERE users.id = ?
Params: 2
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=1
Key: Position #1:
paramno=1
id=[0] ""
is_param=1
param_type=2
This last option is also showing me that there is a value at the end dynamic, however it does not show exactly what that value is, to check if it is the one that should be received
How could I do that in the latter case, I can see instead of the name / position marker, the direct value that is being sent?