Jump to content

get multiple rows


teknojunkey

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.