blinks58 Posted January 13, 2011 Share Posted January 13, 2011 I am trying to convert some PEAR code to ZEND, and haven't worked with ZEND before. The code in question goes like this - //for each row in user table do the following actions while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) { //do the following actions } Any assistance in starting me off by recommending an equivalent ZEND code for that "while" line would be much appreciated. Link to comment https://forums.phpfreaks.com/topic/224254-pear-zend-question/ Share on other sites More sharing options...
trq Posted January 13, 2011 Share Posted January 13, 2011 http://framework.zend.com/manual/en/zend.db.html Link to comment https://forums.phpfreaks.com/topic/224254-pear-zend-question/#findComment-1158656 Share on other sites More sharing options...
blinks58 Posted January 13, 2011 Author Share Posted January 13, 2011 Thanks for that useful reference. Here is the bit of code I've got so far, but the "while" bit is still not working - $stmt = "SELECT usr_username, usr_ldap_authentication FROM " . APP_TABLE_PREFIX . "user"; try { $res = $db->fetchAll($stmt, Zend_Db::FETCH_ASSOC); } catch(Exception $ex) { $log->err($ex); return ''; } print_r($res); //for each row do the following while ($row = $res->current()) { $username = $row['usr_username']; echo $username; } Link to comment https://forums.phpfreaks.com/topic/224254-pear-zend-question/#findComment-1158669 Share on other sites More sharing options...
thehippy Posted January 13, 2011 Share Posted January 13, 2011 array()->current() will not increment the internal array pointer in your while loop. foreach is optimized for this behaviour foreach ($res as $row) { echo $row['usr_username']; } And just to note, its common practise to use query builders in ZF over raw SQL. // Create a Zend_DB_Select object $query = $db->select(); $query->from( APP_TABLE_PREFIX . 'user', array('usr_username', 'usr_ldap_authentication')); try { // Connection and Query Attempt is made here $res = $db->fetchAll($query); } // What you can catch varies per adaptor, catch all catch (Exception $ex) { $log->err($ex); return ''; } foreach ($res as $row) { echo $row['usr_username']; } Link to comment https://forums.phpfreaks.com/topic/224254-pear-zend-question/#findComment-1158771 Share on other sites More sharing options...
blinks58 Posted January 13, 2011 Author Share Posted January 13, 2011 Thanks heaps thehippy! Plain old PHP code works. For some reason, I was thinking there had to be specific Zend code to replace the PEAR code. Wasn't aware of the SQL query builders in Zend either. Regards Link to comment https://forums.phpfreaks.com/topic/224254-pear-zend-question/#findComment-1159089 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.