Home  >  Article  >  Backend Development  >  bindParam and bindValue are parsed using steps in Yii2

bindParam and bindValue are parsed using steps in Yii2

php中世界最好的语言
php中世界最好的语言Original
2018-05-18 11:22:522240browse

This time I will bring you the step-by-step analysis of using bindParam and bindValue in Yii2. What are the precautions for using bindParam and bindValue in Yii2. The following is a practical case, let's take a look.

bindParam() and bindValue() are very similar. The only difference is that the former uses a PHP variablebinding 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.

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

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

I believe you have mastered the method after reading the case in this article, more Please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to make real-time search prompts in PHP JS

PHP sorts arrays according to the size of each key value method

The above is the detailed content of bindParam and bindValue are parsed using steps in Yii2. 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