Am try to looping through a database result set, the problem is that i know i have only one row of data, yet the loop returns 5 rows
This is my method from my model
function get_latest_pheeds() {
$data = $this->user_keywords($this->ion_auth->user_id);
$keyword = $data;
$user_id = $this->ion_auth->user_id;
foreach($keyword as $key => $word) {
$q = "SELECT *,COUNT(pheed_comments.comment_id) as comments
FROM pheeds
LEFT JOIN pheed_comments ON pheed_comments.P_id=pheeds.pheed_id
WHERE pheed LIKE '%$word%' OR user_id='$user_id'
GROUP BY pheeds.pheed_id
ORDER BY datetime DESC";
$result = $this->db->query($q);
$rows[] = $result->result();
}
return $rows;
}
What am i doing wroing
This is because your query has an
ORwhere it should probably have anAND– each query always matches theuser_id='$user_id'.This loop is quite inefficient anyway, I think you want to do something more like this:
If you want your results returned as an array like they were previously, you’ll need to loop over results and fetch each of them into an array key of another array. I can’t do it here because I can’t see your db class…
EDIT: Slight fix in the above code…
ANOTHER EDIT: another fix…