I am trying to write a function where I will get the id’s from a table and then I want to have the all the id’s in the array so that these id’s used in another function to be processed.
I wrote the below but it’s not array method and also it’s printing the id’s twice. I searched online and they all suggest to use mysql_fetch_assoc and remove for each..but in my case I am using the zend adapter fetchAll to get the output. Please let me know how I can get the id’s in the array and also only once so that I can pass this array and each id is processed one by one..right now with what I have is just stopping after the first one is processed. Thanks.
function getID()
{
$sql = 'SELECT user_id FROM users WHERE ready = "1" ';
$idList = $this->getAdapter()->fetchAll($sql);
if(!empty($idList)) {
foreach($idList as $value) {
echo $value['user_id']."\n";
}
}
}
Output
201
223
231
334
201
223
231
334
Try this.
You will then get the array by:
You should figure out why they are being duplicated, but in case you can’t, you can make the array content unique with:
Put that right before
return $result;inside the function. I’ve commented it in the function above because would be much better to find out why it’s being duplicated in the first place. As I noted in a comment to your question, please make sure that the function isn’t being called twice. Also check the database for duplicate entries (although that shouldn’t be possible if ‘user_id’ is a primary key).(Please also note that I renamed the function to getIDs() because it makes no sense to have a getID() function returning multiple values. 🙂 )