Zend_db_adapter Get an instance $ Config = array ( 'Host' => '2017. 0.0.1 ', 'Username' => 'root ', 'Password' => '', 'Dbname' => 'zf ', 'Port' => '123 ', ); $ Db = zend_db: Factory ('mysqli', $ config ); About zend_db Part 1: Read data The fetchall () method is acceptable: 1. Select SQL statement 2. zend_db_select. Object In fact, the following methods can also accept the two parameters: Related arrays are returned by default, You can use setfetchmode () to change the return value. If setfetchmode (zend_db: fetch_obj) returns an object Array
Fetchpairs () is special and returns an array. The key is the value returned by the first column. The value is the value of the second column. Other columns are ignored. For example Age name 10 20 B Returns array (10 => 'A', 20 => 'B ');
The fetchrow () method returns a row. Whether the returned result is an array or is determined by setfetchmode ()? Fetchcol () returns the first column Fetchone () returns the first row and the first column. A value is not an array. Fetchassoc () returns the relevant array, which is equivalent to the return value of fetchall () by default.
Second part: Write Data Insert () The first parameter is the table to be inserted. The second parameter is the related array ("field" => "value ") The data to be inserted is automatically processed, so if you want to disable automatic processing, it is used as a MySQL expression. Zend_db_expr needs to be added during transmission For example: $ ROW = array ( 'Name' => 'curdate ()', 'Address' => New zend_db_expr ('curdate ()') ) In this way, the field name inserts a string of curdate (), and the address inserts a time value (the result of curdate) Lastinsertid () If the table contains an auto-increment field, this value is returned after data is inserted.
Third, update data Update () First parameter, data table The second parameter, the updated value The third parameter, condition, and expression For example, $ db-> Update ('test', $ row, "name = 'terry 'or address = 'terry '"); If the third parameter is in the array format, such as $ where = array ("name = 'terry '", "address = 'terry '"); And is used to connect each value. Therefore, it seems convenient to write a statement directly.
Part 4: Delete Delete () First parameter, data table Second parameter, Condition Same as update ()
The zend_db_adapter obtains an object, adds, deletes, updates, and inserts data. Which of the following are commonly used in current work?
Others Listtables () is used to list all tables in the current database. Describetable () lists the fields of a table.
Zend_db_select This is also common in work Get an object $ Select = $ db-> select ()
$ Select-> from () From has three parameters. First: Which table Second: Which field represents all fields with *, or put the field to be displayed in an array? Select 'test'. 'name' as 'n', (age + 5) as 'A' from 'test' sometimes note that adding () indicates this Expression Third: Which database
$ Select-> join () The first parameter, the table to join, The second parameter is the connection condition. The third parameter indicates the fields to be listed in the jion table. If it is array (), no columns are required. If it is null, all fields are listed.
$ Select-> where () $ Select-> orwhere () Put all the conditions, whether OR, and in a sentence. The separated Directory provides ?, Then replace a string, as shown in figure Where ('price>? ', $ Minimumprice );
$ Select-> group () Group $ Select-> having () Grouping query data Conditions
$ Select-> Order () Sort columns by which columns are represented as arrays (multiple fields) or strings (one Field)
$ Select-> limit (a, B) obtains the length of a from the nth value. Note that a and B must be integers. If a is written as "a, B", only a takes effect, meaning that a is returned from 0th.
$ Select-> distinct () No parameter. Remove duplicate values. Sometimes it is the same as the result returned by groupby.
Execute SELECT query $ Stmt = $ db-> query ($ select );
$ Result = $ stmt-> fetchall (); Or use $ Stmt = $ select-> query (); $ Result = $ stmt-> fetchall (); If you use $ Db-> fetchall ($ select) results are the same
Zend_db_table Must inherit from zend_db_table_abstract Table interface. Only one table can be operated. zend_db and zend_db_select on the front can be operated on multiple tables. You can construct multiple table objects to operate multiple tables at the same time. There are multiple methods to specify the table name and database name, such Class test extends zend_db_table_abstract { Method 1 Protected $ _ name = 'test '; Protected $ _ schema = 'zf '; Method 2 Protected $ _ name = 'zf. test '; } Or Class test extends zend_db_table_abstract {} Method 1 $ Test = new test (Array ('name' => 'test', 'schema' => 'zf ')) Method 2 $ Test = new test (Array ('name' => 'zf. test ')) Of course, you can also leave the database Unspecified. The default value is the database in zend_db.
Before using zend_db_table, you must specify a Database Connector to indicate an active database connection.
There are three methods 1. Use 'db' in the construction to specify if $ test = new test (Array ('db' => $ dB )) Second, zend_db_table_abstract: setdefaadapter adapter ($ dB); however, only one Fixed database connection Third: Zend_registry: Set ('My _ db', $ dB ); $ Table = new bugs (Array ('db' => 'my _ db ')); This is a common method.
Insert data Insert () Update Data Update {} ...... In fact, these methods call the same method in zend_db. The first parameter is missing.
Another method that is more than zend_db is the find method. You can query data by primary key. The data returned by this method is not an array, Object rowset
Primary key combination Class bugsproducts extends zend_db_table_abstract { Protected $ _ name = 'bugs _ products '; Protected $ _ primary = array ('bug _ id', 'product _ id '); } $ Table = new bugsproducts (); // Find a single row with a compound primary key // Returns a rowset $ Rows = $ table-> Find (1234, 'abc '); // Find multiple rows with compound primary keys // Also returns a rowset $ Rows = $ table-> Find (Array (1234,567 8), array ('abc', 'def '));
The fetchall method in zend_db_table is also the returned object rowset Parameters Fetchall ($ where = NULL, $ order = NULL, $ COUNT = NULL, $ offset = NULL)
Fetchrow returns an object row where the toarray () method can convert the object into an array. You can also use the createrow () method to create a row, specify the data, and then save The former is equivalent to update, and the latter is equivalent to insert Delete is used to delete, see http://framework.zend.com/manual/en/zend.db.table.row.html for details
You can use fecthrow to directly modify the read data and save the data to the database using the Save method.
Info () returns table-related content, equivalent to describetable ('tablename') in zend_db reprinted: http://hi.baidu.com/48238398/blog/item/9afb762a5c850a3b5243c1ec.html |