Yii Data Processing Dao

Source: Internet
Author: User

The yii framework can process data in two ways: Ar and Dao (data processing objects)

The former mainly deals with simple SQL statements.

The latter is used to process complex SQL statements.

 

The two methods are based on PDO and PHP Data Object: PHP Data Object.

PDO is used to process various databases (MySQL, Oracle, SQL Server, and so on) for compatibility with PHP)

We define an SQL statement that can be run in different databases of MySQL, Oracle, and sqlserver.

 

AR: processing data cactiverecord

Dao Data Processing cdbcommand cdbdatareader

Query () for query

Execute () for addition, modification, and deletion

 

 

PDO preprocessing adds, updates, and deletes information.




 

/** Data Processing Dao for Data Query */function actioncat () {// read data in DAO mode $ SQL = "select * from {goods}"; // 1. create a DaO object $ d_obj = yii: APP ()-> DB-> createcommand ($ SQL); // 2. run the SQL statement // query: the DaO object query () returns a result object cdbdatareader // queryall () to directly obtain all record results // queryrow () obtain the first record result // querycolum () obtain the record result of the first column // non-query: Execute () // read (): use the methods in the cdbdatareader class to obtain specific results // readall (): obtain all record results // $ data_obj = $ d_obj-> query (); // $ info = $ data_obj-> Read (); // obtain a record result, a one-dimensional array // $ info = $ data_obj-> readall (); // obtain all record results, two-dimensional array // $ info = $ d_obj-> queryall (); // directly obtain all the information results // $ info = $ d_obj-> queryrow (); // directly obtain the first record result // var_dump ($ info ); // var_dump ($ data_obj);}/** add data using Dao */function actioncat2 () {$ SQL = "insert into {goods} (goods_name, goods_price) values (: name,: price) "; $ d_obj = yii: APP ()-> DB-> createcommand ($ SQL); $ name =" apple678 "; $ price = 5050; // bindparam (': name', $ name, PDO: param_str) to bind the two Defined variables to the placeholder ); $ d_obj-> bindparam (': pric', $ price, PDO: param_int); $ d_obj-> execute (); $ name = "Nokia 678 "; $ price = 3999; // bindparam (': name', $ name, PDO: param_str) to bind the two Defined variables to the placeholder ); $ d_obj-> bindparam (': price', $ price, PDO: param_int); $ d_obj-> execute (); // $ num = $ d_obj-> execute (); // The number of currently affected records will be returned // you can use PDO preprocessing to add information // $ SQL = "insert into {Article} (article_title, article_content) values (: title,: Content) "; // $ command = $ connection-> createcommand ($ SQL); // $ Title =" RR "; // $ content = "RR"; // $ command-> bindparam (": Title", $ title, PDO: param_str ); // $ command-> bindparam (": content", $ content, PDO: param_str); // $ rowcount = $ command-> execute ();}



Yii Data Processing Dao

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.