bindParam() 和 bindValue() 非常相似。唯一的区别就是前者使用一个 PHP 变量绑定参数, 而后者使用一个值。对于那些内存中的大数据块参数,处于性能的考虑,应优先使用前者。本文主要和大家分享一篇bindParam和bindValue的区别以及在Yii2中的使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧,希望能帮助到大家。
根据id查询一条数据,并对id进行过滤:
$id = 1; $result = Yii::$app->db->createCommand("select * from product where id=:id")->bindParam(":id",$id,\PDO::PARAM_INT)->queryAll(); $result = Yii::$app->db->createCommand("select * from product where id=:id")->bindParam(":id",$id,\PDO::PARAM_STR)->queryAll();
更新一条数据:
$id = 1; $name = 'xiaoming'; $result = Yii::$app->db->createCommand("update product set name=:name where id=:id")-> bindParam(':id',$id,\PDO::PARAM_INT)->bindParam(':name',$name,\PDO::PARAM_INT)->execute();
以下写法在会报错
$result = Yii::$app->db->createCommand()->delete('product', ['name'=>':value'],'id=:id')->bindValue(':id',1,\PDO::PARAM_INT)-> bindParam(':value',$user,\PDO::PARAM_INT)->execute();
相关推荐:
php pdo中PDOStatement 类的bindParam和bindValue方法的区别
以上就是bindParam和bindValue的区别详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号