Php 使用execute数组与直接bind_param方法相比的优势

Php 使用execute数组与直接bind_param方法相比的优势,php,data-binding,pdo,Php,Data Binding,Pdo,与直接绑定参数方法相比,使用execute数组的优势是什么 $stmtt = $conn->prepare($sql); $stmt->bindParam(':title', $_POST['title '], PDO::PARAM_STR); $stmt->execute(); 我可以想到两个好处。(1) 您可以使用bindParam或bindValue指定数据类型,而使用execute array会将所有内容视为字符串。在某些情况下,能够指定数据类型非常方便。例如:

与直接绑定参数方法相比,使用execute数组的优势是什么

$stmtt = $conn->prepare($sql); 

$stmt->bindParam(':title', $_POST['title '], PDO::PARAM_STR); 
$stmt->execute();

我可以想到两个好处。(1) 您可以使用
bindParam
bindValue
指定数据类型,而使用execute array会将所有内容视为字符串。在某些情况下,能够指定数据类型非常方便。例如:

 // Return the correct result
 $sh = $db->prepare("SELECT * FROM items LIMIT :offset,:length");
 $sh->bindValue(":offset", 0, PDO::PARAM_INT);
 $sh->bindValue(":length", 10, PDO::PARAM_INT);
 $sh->execute();

 // Does not return any result, unless you set PDO::ATTR_EMULATE_PREPARES to FALSE
 $sh = $db->prepare("SELECT * FROM items LIMIT :offset,:length");
 $sh->execute(array(
    "offset" => 0,
    "length" => 10
 ));
$sh = $db->prepare("INSERT INTO news_tags(news_id, tag) VALUES(:id, :tag)");
$sh->bindValue(":id", 2);
$sh->bindParam(":tag", $tag);

foreach($tags as $value) {
    $tag = $value;
    $sh->execute();
}
(2) 如果要多次插入,
bindParam
会更容易。例如:

 // Return the correct result
 $sh = $db->prepare("SELECT * FROM items LIMIT :offset,:length");
 $sh->bindValue(":offset", 0, PDO::PARAM_INT);
 $sh->bindValue(":length", 10, PDO::PARAM_INT);
 $sh->execute();

 // Does not return any result, unless you set PDO::ATTR_EMULATE_PREPARES to FALSE
 $sh = $db->prepare("SELECT * FROM items LIMIT :offset,:length");
 $sh->execute(array(
    "offset" => 0,
    "length" => 10
 ));
$sh = $db->prepare("INSERT INTO news_tags(news_id, tag) VALUES(:id, :tag)");
$sh->bindValue(":id", 2);
$sh->bindParam(":tag", $tag);

foreach($tags as $value) {
    $tag = $value;
    $sh->execute();
}

我也在想同样的事情。也许这是唯一的好处。