Jump to content

Recommended Posts

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.