【问题标题】:PDO::PARAM_INT in PDOStatement::executePDOStatement::execute 中的 PDO::PARAM_INT
【发布时间】:2017-07-25 18:17:59
【问题描述】:

有没有办法在PDOStatement::execute 中指定PDO::PARAM_INT

我习惯做以下事情……

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

但是,有时插入的值需要是整数..

我知道可以使用bindValuebindParam ..

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

但是,我想要类似的东西:

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

这存在吗?

【问题讨论】:

  • PDOStatement::execute() 只接受一个参数 - $input_parameters 的数组,因此您需要明确使用 bind* 函数。
  • 为什么不在验证过程中确保它是一个整数?
  • 手册中有一些示例只是针对您的要求而制作的,您没有看过/尝试过吗?
  • @Fred-ii- 我没有。你说的是 php.net 手册吗?
  • @TT4M.C 是的,我在下面发布了一个社区 wiki 答案,您可以查看。

标签: php mysql pdo prepared-statement


【解决方案1】:

示例取自以下内容,它们正是您想要在此处执行的操作。

旁注:我将其作为社区 wiki 答案发布,因为我确实将它们从现有代码中提取出来。

来自this user contributed notebindValue()

/*
   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);
    }

来自this user contributed notebindParam()

<?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.
* */
?>

【讨论】:

    猜你喜欢
    • 2012-03-03
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 2016-04-12
    • 1970-01-01
    • 2019-06-22
    相关资源
    最近更新 更多