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. Quote 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 Quote 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; } Quote 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']; } Quote 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 Quote Link to comment https://forums.phpfreaks.com/topic/224254-pear-zend-question/#findComment-1159089 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.