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