TT4M.C TT4M.C - 3 years ago 135
MySQL Question

PDO::PARAM_INT in PDOStatement::execute

Is there a way to specify

PDO::PARAM_INT
in
PDOStatement::execute
?

I am used to doing the following ...

$STH = $DBH->prepare('INSERT INTO items (name, description) VALUES (:name, :description)');
$STH->execute(array(':name' => $name, ':description' => $description));


However, there has come a time where the inserted value needs to be an integer ..

I understand that one could use
bindValue
or
bindParam
..

$STH->bindParam(':price', $price, PDO::PARAM_INT);


However, I want something like:

$STH->execute(array(':price' => array('value' => $price, 'type' => PDO::PARAM_INT)));


Does this exist?

Answer Source

Examples taken from the following, whioh do exactly what you want to do here.

Side note: I am posting this as a community wiki answer and since I did pull them off existing code.

From this user contributed note for bindValue():

/*
   method for pdo class connection, you can add your cases by    yourself and use it.
*/
class Conn{
....
....
private $stmt;
public function bind($parameter, $value, $var_type = null){
        if (is_null($var_type)) {
            switch (true) {
                               case is_bool($value):
                    $var_type = PDO::PARAM_BOOL;
                    break;
                case is_int($value):
                    $var_type = PDO::PARAM_INT;
                    break;
                case is_null($value):
                    $var_type = PDO::PARAM_NULL;
                    break;
                default:
                    $var_type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($parameter, $value, $var_type);
    }

From this user contributed note for bindParam():

<?php
/**
* @param string $req : the query on which link the values
* @param array $array : associative array containing the values ??to bind
* @param array $typeArray : associative array with the desired value for its corresponding key in $array
* */
function bindArrayValue($req, $array, $typeArray = false)
{
    if(is_object($req) && ($req instanceof PDOStatement))
    {
        foreach($array as $key => $value)
        {
            if($typeArray)
                $req->bindValue(":$key",$value,$typeArray[$key]);
            else
            {
                if(is_int($value))
                    $param = PDO::PARAM_INT;
                elseif(is_bool($value))
                    $param = PDO::PARAM_BOOL;
                elseif(is_null($value))
                    $param = PDO::PARAM_NULL;
                elseif(is_string($value))
                    $param = PDO::PARAM_STR;
                else
                    $param = FALSE;

                if($param)
                    $req->bindValue(":$key",$value,$param);
            }
        }
    }
}

/**
* ## EXEMPLE ##
* $array = array('language' => 'php','lines' => 254, 'publish' => true);
* $typeArray = array('language' => PDO::PARAM_STR,'lines' => PDO::PARAM_INT,'publish' => PDO::PARAM_BOOL);
* $req = 'SELECT * FROM code WHERE language = :language AND lines = :lines AND publish = :publish';
* You can bind $array like that :
* bindArrayValue($array,$req,$typeArray);
* The function is more useful when you use limit clause because they need an integer.
* */
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download