php的PDO类中bindValue和bindParam的区别

// bindParam是绑定一个PHP变量到一个SQL参数.是引用方式传递
// 所以你可以改变变量值,再执行.就赋给SQL语句不同的值
// 注意与bindValue的区别

try {   
$sql = 'UPDATE `pdo` set `user` = :user, `email` = :email WHERE `id` = :id';
$pre = $dbh->prepare($sql);   
// 此处bindParam.执行一次更改id=2,执行一次更改id=3.结果不一样
$user = "ncat2";
$email = "[email protected]";
$id = 2;
$pre->bindParam(':user', $user, PDO::PARAM_STR);
$pre->bindParam(':email', $email, PDO::PARAM_STR);
$pre->bindParam(':id', $id, PDO::PARAM_INT);
$pre->execute();
$user = "ncat3";
$email = "[email protected]";
$id = 3;
$pre->execute();
unset($user, $email, $id);
// 此处bindValue. 执行两次id=4的SQL
$user = "ncat4";
$email = "[email protected]";
$id = 4;
$pre->bindValue(':user', $user, PDO::PARAM_STR);
$pre->bindValue(':email', $email, PDO::PARAM_STR);
$pre->bindValue(':id', $id, PDO::PARAM_INT);
$pre->execute();
$user = "ncat5";
$email = "[email protected]";
$id = 5;
$pre->execute();
} catch (PDOException $e)
{var_dump($e);}

你可能感兴趣的:(职场,休闲,PDP,bindparam,bindvalue)