Jump to content

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

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.