这次给大家带来bindParam和bindValue在Yii2中的使用详解,bindParam和bindValue在Yii2中使用的注意事项有哪些,下面就是实战案例,一起来看一下。
bindParam() 和 bindValue() 非常相似。唯一的区别就是前者使用一个 PHP 变量绑定参数, 而后者使用一个值。对于那些内存中的大数据块参数,处于性能的考虑,应优先使用前者。
根据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中文网其它相关文章!
推荐阅读:
千博购物系统.Net能够适合不同类型商品,为您提供了一个完整的在线开店解决方案。千博购物系统.Net除了拥有一般网上商店系统所具有的所有功能,还拥有着其它网店系统没有的许多超强功能。千博购物系统.Net适合中小企业和个人快速构建个性化的网上商店。强劲、安全、稳定、易用、免费是它的主要特性。系统由C#及Access/MS SQL开发,是B/S(浏览器/服务器)结构Asp.Net程序。多种独创的技术使









