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