2

My question wasn't clear so I'm editing a bit.

I have a website where people can bid on stuff and on their account page they can track there bids. But if one person bids twice on a product they see that product twice. So I only want to show there highest bid per product.

This is my query:

$objDatabaseAds->prepare('SELECT * FROM bieden WHERE ownerid = :ownerid ORDER BY prijs DESC'); 

Greetings,

Bram

EDIT

$objGetHighest = $objDatabaseAds->prepare('SELECT * FROM bieden WHERE ownerid = :ownerid GROUP BY adid'); $objGetHighest->bindParam('ownerid', $member_id); $objGetHighest->execute(); $objGetHighestFetch = $objGetHighest->fetchAll(PDO::FETCH_ASSOC); foreach ($objGetHighestFetch as $key) { $adID = $key['adid']; //This echoes 3 values which is good!! $objGetBid = $objDatabaseAds->prepare('SELECT * FROM bieden WHERE adid = :adid'); $objGetBid->bindParam('adid', $adID); $objGetBid->execute(); $objGetBidFetch = $objGetBid->fetchAll(PDO::FETCH_ASSOC); } 

So this is what I got right now. The $adID echoes 3 values out of the 4 ads where 2 of them have the same ID so this is good. But then I do another query and then I get then I see the bids of the ID which he shows once.

Table

8
  • use GROUP BY product_ID Commented Oct 6, 2016 at 9:17
  • Thanks for your reply, when I use this in my query I get 2 result instead of 3 is this the right syntax? SELECT * FROM bieden WHERE ownerid = :ownerid GROUP BY adid = :adid ORDER BY prijs DESC Commented Oct 6, 2016 at 9:28
  • do you want to select all of ownerid with specific value? Commented Oct 6, 2016 at 9:35
  • Yes because this is on the account page so the owner only gets to see his own bids. And then I want to show the owner highest bids. Commented Oct 6, 2016 at 9:41
  • if you want to show all the bids for one(1) owner who had login to your app, just remove the GROUP BY. use SELECT * FROM bieden WHERE ownerid = 'ownerid value' ORDER BY prijs DESC. Thats all, it will give you all bids in a high sort Commented Oct 6, 2016 at 9:54

2 Answers 2

2

Try this. just add GROUP BY adid, it works like you want. Cheers! hope helpfully.

$objGetHighest = $conn->prepare("SELECT * FROM tabs WHERE ownerid = :ownerid GROUP BY adid"); $objGetHighest->bindParam('ownerid', $member_id); $objGetHighest->execute(); $objGetHighestFetch = $objGetHighest->fetchAll(PDO::FETCH_ASSOC); foreach ($objGetHighestFetch as $key) { $adID = $key['adid']; //This echoes 3 values which is good!! //echo $adID."<br />"; $objGetBid = $conn->prepare("SELECT * FROM tabs WHERE adid = '$adID' GROUP BY adid "); $objGetBid->execute(); $objGetBidFetch = $objGetBid->fetchAll(PDO::FETCH_ASSOC); print_r($objGetBidFetch); } 
Sign up to request clarification or add additional context in comments.

Comments

2

if want to get one higher rows, you can try this, hope will help you. Cheers!

$query = $objDatabaseAds->prepare('SELECT * FROM bieden WHERE ownerid = :ownerid ORDER BY prijs DESC LIMIT 1'); $query->execute(); while($data = $query->fetch(PDO::FETCH_ASSOC)){ echo $data['column_name']; echo $data['ownerid']; } 

1 Comment

Sorry my answer for your previous question. so, what do you want is just get one higher rows from your table? if that correct, just add your query LIMIT 1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.