Yii uses DAO to operate databases

Source: Internet
Author: User
Yii uses DAO to operate databases

DAO obtains a record:

?
123456 $ SQL = "select title, id from {blog} where id =: id"; $ command = Yii: app () -> db-> createCommand ($ SQL); $ id = (int) $ _ GET ['id']; $ command-> bindParam (": id", $ id, PDO: PARAM_INT); $ data = $ command-> queryRow (); // The bindParam () method is very similar to bindValue. The only difference is that the former uses a PHP variable to bind a parameter, while the latter uses a value. For the big data block parameters in the memory, the performance should be considered first.

Obtain multiple records:

?
123 $ SQL = "select title, id from {blog}"; $ command = Yii: app ()-> db-> createCommand ($ SQL ); $ data = $ command-> queryAll ();

Obtain a field of a record:

?
12345 $ SQL = "select title from {blog} where id =: id"; $ command = Yii: app ()-> db-> createCommand ($ SQL ); $ id = (int) $ _ GET ['id']; $ command-> bindParam (": id", $ id, PDO: PARAM_INT ); $ title = $ command-> queryScalar ();

Use transactions:

?
123456789101112131415161718192021 $ Transaction = Yii: app ()-> db-> beginTransaction (); try {$ SQL = "update {blog} set title = 'abc' where id = 2"; Yii: app ()-> db-> createCommand ($ SQL) -> execute (); $ transaction-> commit ();} catch (Exception $ e) {$ transaction-> rollback ();}

Insert data:

?
123456789 $ SQL = "insert into {blog} (title) values (: title)"; $ command = Yii: app () -> db-> createCommand ($ SQL); $ title = 'abc'; $ command-> bindParam (": title", $ title, PDO: PARAM_STR ); $ command-> execute (); $ title = 'bbbbbb'; $ command-> bindParam (": title", $ title, PDO: PARAM_STR ); $ command-> execute ();

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.