php - PDO::PARAM_INT in PDOStatement::execute -
is there way specify pdo::param_int
in pdostatement::execute
?
i used doing following ...
$sth = $dbh->prepare('insert items (name, description) values (:name, :description)'); $sth->execute(array(':name' => $name, ':description' => $description));
however, there has come time inserted value needs integer ..
i understand 1 use bindvalue
or bindparam
..
$sth->bindparam(':price', $price, pdo::param_int);
however, want like:
$sth->execute(array(':price' => array('value' => $price, 'type' => pdo::param_int)));
does exist?
examples taken following, whioh want here.
side note: posting community wiki answer , since did pull them off existing code.
- http://php.net/manual/en/pdostatement.bindparam.php
- http://php.net/manual/en/pdostatement.bindvalue.php
from this user contributed note bindvalue()
:
/* method pdo class connection, can add cases , 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 bindparam()
:
<?php /** * @param string $req : query on link values * @param array $array : associative array containing values ??to bind * @param array $typearray : associative array desired value corresponding key in $array * */ function bindarrayvalue($req, $array, $typearray = false) { if(is_object($req) && ($req instanceof pdostatement)) { foreach($array $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 * code language = :language , lines = :lines , publish = :publish'; * can bind $array : * bindarrayvalue($array,$req,$typearray); * function more useful when use limit clause because need integer. * */ ?>
Comments
Post a Comment