7

Is there an easy way to echo the value stored in a bound parameter.

$sql ="call storedproc(:firstname, :lastname)";
$stmt = $this->DBH->prepare($sql);
$stmt->bindParam(':firstname', $fname);
$stmt->bindParam(':lastname', $lname);

//I want to do this
echo $stmt->firstname;
$stmt->execute;

2 Answers 2

8

If you only want to "see" what's going on then there's PDOStatement->debugDumpParams():

Dumps the informations contained by a prepared statement directly on the output. It will provide the SQL query in use, the number of parameters used (Params), the list of parameters, with their name, type (paramtype) as an integer, their key name or position, the value, and the position in the query (if this is supported by the PDO driver, otherwise, it will be -1).
Sign up to request clarification or add additional context in comments.

2 Comments

It should be noted that currently this function does not print the value stored in the params without applying a patch per PHP Bug #52384 bugs.php.net/…
The quote above has been updated in the php manual. The removed "the value" from it, the most important part... :-(
0
<?php 
    function parms($string,$data) {
        $indexed=$data==array_values($data);
        foreach($data as $k=>$v) {
            if(is_string($v)) $v="'$v'";
            if($indexed) $string=preg_replace('/\?/',$v,$string,1);
            else $string=str_replace(":$k",$v,$string);
        }
        return $string;
    }

    $string='INSERT INTO stuff(name,value) VALUES (?,?)';
    $data=array('Fred',23);

    $string='INSERT INTO stuff(name,value) VALUES (:name,:value)';
    $data=array('name'=>'Fred','value'=>23);

    print parms($string,$data);

    $update = "update registration";
    $update .= " set status=:statusid";     
    $update .= " where refid=:refid";               
    $stmt = $db->prepare($update);

    $data=array('statusid' => $statusid,'refid' => $refid);
    echo parms($update,$data); // checking bind values
    exit; 

    $stmt->execute(array ('statusid' => $statusid,'refid' => $refid));
?>

//output

update registration set status='all' where refid='45'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.