views:

15

answers:

1

Just can't seem to print the binded values without executing the query. Looking to debug the query before execution. Any tips? I know I'm overlooking something simple, ugh...

$field1 = 'one';
$field2 = 'two';
$field3 = 'three';

$fields  = 'SET ';
$fields .= 'field1 = ?, ';
$fields .= 'field2 = ?, ';
$fields .= 'field3 = ? ';

$vals[] = $field1;
$vals[] = $field2;
$vals[] = $field3;

$sql = 'UPDATE table_name '.$fields.' WHERE id = 123';
$dbh = $db->prepare($sql);

// this binds and executes the query but I would like to print the query with the bind values before executing
$results = $db->execute($dbh, $vals); 

UPDATE:

I would do something like this with sprinf

$field1 = 'one';
$field2 = 'two';
$field3 = 'three';

$fields  = 'SET ';
$fields .= 'field1 = %s, ';
$fields .= 'field2 = %s, ';
$fields .= 'field3 = %s ';

$vals[] = $field1;
$vals[] = $field2;
$vals[] = $field3;

$sql = 'UPDATE table_name '.$fields.' WHERE id = 123';

$query = sprintf($sql, $field1, $field2, $field3);
echo "Query before execution: ".$query."<br />";
+1  A: 

You can't get the values inside the query like that. The way the server handles prepared queries is different.

The best you could do is:

echo $sql;
print_r($vals);

http://stackoverflow.com/questions/3754530/retrieve-or-simulate-full-query-from-pdo-prepared-statement

Galen
no I understand I can print them in separate lines, I want to print the query as it would be executed, before I execute
Phill Pafford
that's why i stated that you cant do that
Galen
can I mix PDO and Pear? something like $dbh->bindValue(':field1', $field1, PDO::PARAM_STR);
Phill Pafford
Looks like there is a bug, ugh. no real solution to do what I wanted
Phill Pafford