Prev | Current Page 144 | Next

Rob Allen, Nick Lo, and Steven Brown

"Zend Framework in Action"

4.
Listing 5.4: Updating multiple rows using Zend_Db_Adapter
// update a user
$data = array( |#1
'country' => 'United Kingdom' |
); |
$table = 'users';
$where = $db->quoteInto('country = ?', 'UK'); #2
$db->update($table, $data, $where);
(annotation) <#1 Fields to be updated only.>
Licensed to Menshu You
Please post comments or corrections to the Author Online forum at
http://www.manning-sandbox.com/forum.jspa?forumID=329
(annotation) <#2 Quote strings in where clause.>
Again, update() automatically quotes the data in the $data array, but does not automatically quote the data
in the $where clause, so we use quoteInto() to ensure that the data is safe for use with the database.
Deleting from the database works in exactly the same manner, only we just need the table and the where
clause, so for deleting a particular user the code would be:
$table = 'users';
$where = 'id = 1';
$rows_affected = $db->delete($table, $where);
Obviously, we need to quote strings in the $where clause again. In all cases insert(), update() and delete()
return the number of rows affected by the operation. We can now look at how to handle database specific
differences.
5.1.4 Handling database specific differences
All databases are not equal and this is especially true of their interpretation of SQL and the additional functions
they provide to allow access to the more advanced features.


Pages:
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
roll-up nowoczesne meble Accept podl Firmy chiƄskie opolskie