teknojunkey Posted October 13, 2009 Share Posted October 13, 2009 Hi I want to get multiple rows from a db I am using this for the db query public function getCeleb($product_id) { $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_celeb WHERE product_id = '" . (int)$product_id . "'"); foreach ($query->rows as $result) { $celeb_id = $result['celeb_id']; } return $celeb_id; } and this to display the returned data 'celeb_id' => $result['celeb_id'] print_r ($celeb_id); the problem is i only get one result from the database and i have multiple entries with the same id thanks in advance Link to comment https://forums.phpfreaks.com/topic/177563-get-multiple-rows/ Share on other sites More sharing options...
shorty3 Posted October 13, 2009 Share Posted October 13, 2009 u need to add $i++; at the end and ,$i in the brackets Link to comment https://forums.phpfreaks.com/topic/177563-get-multiple-rows/#findComment-936231 Share on other sites More sharing options...
teknojunkey Posted October 13, 2009 Author Share Posted October 13, 2009 can you explain for stupid please Link to comment https://forums.phpfreaks.com/topic/177563-get-multiple-rows/#findComment-936254 Share on other sites More sharing options...
PFMaBiSmAd Posted October 13, 2009 Share Posted October 13, 2009 The line of code $celeb_id = $result['celeb_id']; is overwriting $celeb_id each pass through the foreach() loop. You only get the last value out. If you want an array of all the values, you need to build an array - $celeb_id[] = $result['celeb_id']; Link to comment https://forums.phpfreaks.com/topic/177563-get-multiple-rows/#findComment-936260 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.