note 104939 added to pdostatement.bindvalue
| From: | contact | Date: | Sun, 17 Jul 2011 12:19:43 +0000 |
| Subject: | note 104939 added to pdostatement.bindvalue | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181626@lists.php.net to get a copy of this message | ||
This function is useful for bind value on an array. You can specify the type of the value in advance
with $typeArray.
<?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.
* */
?>
----
Server IP: 69.147.83.197
Probable Submitter: 88.189.8.32
----
Manual Page -- http://www.php.net/manual/en/pdostatement.bindvalue.php
Edit -- https://master.php.net/note/edit/104939
Del: integrated -- https://master.php.net/note/delete/104939/integrated
Del: useless -- https://master.php.net/note/delete/104939/useless
Del: bad code -- https://master.php.net/note/delete/104939/bad+code
Del: spam -- https://master.php.net/note/delete/104939/spam
Del: non-english -- https://master.php.net/note/delete/104939/non-english
Del: in docs -- https://master.php.net/note/delete/104939/in+docs
Del: other reasons-- https://master.php.net/note/delete/104939
Reject -- https://master.php.net/note/reject/104939
Search -- https://master.php.net/manage/user-notes.php