Home  >  Article  >  Backend Development  >  Detailed explanation of the difference between bindParam and bindValue

Detailed explanation of the difference between bindParam and bindValue

小云云
小云云Original
2018-03-14 09:30:182170browse

bindParam() and bindValue() are very similar. The only difference is that the former uses a PHP variable to bind the parameter, while the latter uses a value. For those parameters with large data blocks in memory, for performance reasons, the former should be used first. This article mainly shares with you the difference between bindParam and bindValue and a detailed explanation of their use in Yii2. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.

Query a piece of data based on id and filter the 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();

Update a piece of data:

$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();

The following writing method will report an error

$result = Yii::$app->db->createCommand()->delete('product',
['name'=>':value'],'id=:id')->bindValue(':id',1,\PDO::PARAM_INT)->
bindParam(':value',$user,\PDO::PARAM_INT)->execute();

Related recommendations:

The difference between the bindParam and bindValue methods of the PDOStatement class in php pdo

The above is the detailed content of Detailed explanation of the difference between bindParam and bindValue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn