Bindparam and Bindvalue using step resolution in YII2

Source: Internet
Author: User
This time to bring Bindparam and Bindvalue in Yii2 in the use of steps to resolve, Bindparam and Bindvalue in the Yii2 used in the attention of what, the following is the actual case, together to see.

Bindparam () and Bindvalue () are very similar. The only difference is that the former uses a PHP variable binding parameter, and the latter uses a value. For those large data block parameters in memory, in the performance considerations, the former should be used preferentially.

Query one 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 ::P aram_int)->queryall (); $result = Yii:: $app->db->createcommand ("SELECT * from product where Id=:id") Bindparam (": id", $id, \pdo::P aram_str)->queryall ();

Update one piece of data:

$id = 1; $name = ' xiaoming '; $result = Yii:: $app->db->createcommand ("Update product set Name=:name where Id=:id")-> ; Bindparam (': Id ', $id, \pdo::P aram_int)->bindparam (': Name ', $name, \pdo::P aram_int)->execute ();

The following wording will be an error

$result = Yii:: $app->db->createcommand ()->delete (' Product ', [' name ' = ': Value '], ' id=:id ') Bindvalue (': Id ', 1,\pdo::P aram_int)->bindparam (': Value ', $user, \pdo::P aram_int)->execute ();

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Php+js How to make real-time search hints

Php method for sorting arrays based on the size of a key value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.