While Statement to only run once for an ID

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

While Statement to only run once for an ID

Post by MikeD »

Back with another silly question. (Sorry) :(

Trying to create a mybids section that shows all the items you've bid on in the last 24 hours.

I tried doing this by creating another table, and it records everytime you bid.

So, when I create the while statement, it will show the same item multiple times, I'd like it to only show it once. Any way to do this?

Here's what the queries look like currently..

Code: Select all

$mybids="SELECT `aucid` FROM `playerbids` WHERE `pid`='$pid'";
if ($sult = mysqli_query($db,$mybids))
  { 
    while ($bid=mysqli_fetch_row($sult))
      {

        $aucweps="SELECT * FROM `playermarket` WHERE `aucid`='$bid[0]' ORDER BY `aucend`";
          if ($result = mysqli_query($db,$aucweps))
            {
              while ($row = mysqli_fetch_row($result))
                {
                }
            }
       }
  }
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: While Statement to only run once for an ID

Post by Jackolantern »

I am not sure if it is happening in the script that is showing the items. Rather, I am wondering if it is happening from where you are storing the data. Do you have multiples showing up in the database? I don't see any problems here. You are using WHILE statements to run through all the results, so it should only be pulling out what is in the db once for each entry. However, you didn't seem to post the code to show the data (ie, format it into HTML). That could potentially have a problem in it if the items are recorded only once each in the database.
The indelible lord of tl;dr
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: While Statement to only run once for an ID

Post by Winawer »

Based on just that script it's difficult to say what's going on, but try

Code: Select all

$mybids="SELECT DISTINCT `aucid` FROM `playerbids` WHERE `pid`='$pid'"; 
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: While Statement to only run once for an ID

Post by MikeD »

Winawer wrote:Based on just that script it's difficult to say what's going on, but try

Code: Select all

$mybids="SELECT DISTINCT `aucid` FROM `playerbids` WHERE `pid`='$pid'"; 

Sorry I didn't explain it well. But this is what I needed, thanks. Why did that never pop up on google lol.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: While Statement to only run once for an ID

Post by Jackolantern »

Were you searching for PHP solutions or MySQL solutions? If you were looking for PHP solutions, that is probably why.
The indelible lord of tl;dr
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: While Statement to only run once for an ID

Post by MikeD »

Jackolantern wrote:Were you searching for PHP solutions or MySQL solutions? If you were looking for PHP solutions, that is probably why.
It was kind of hard to explain what I needed, but this query was exactly what I was looking for.
$mybids="SELECT DISTINCT `aucid` FROM `playerbids` WHERE `pid`='$pid'";
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: While Statement to only run once for an ID

Post by Jackolantern »

But are you sure you aren't storing more than one copy of each item? That query will only pull out one copy of each. It would do nothing related to entering the data. If the items are supposed to be unique, using DISTINCT is only a band-aid that doesn't fix the problem.
The indelible lord of tl;dr
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: While Statement to only run once for an ID

Post by MikeD »

Jackolantern wrote:But are you sure you aren't storing more than one copy of each item? That query will only pull out one copy of each. It would do nothing related to entering the data. If the items are supposed to be unique, using DISTINCT is only a band-aid that doesn't fix the problem.

What happens is, when you place a bid on an item, each time you do it enters a new row into the `playerbids` table. So yes, it can come up with the same item multiple times, but it helps me keep a log of who's bidding on what to see if there is any cheating involved.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: While Statement to only run once for an ID

Post by Jackolantern »

Ohhh, ok, then if the table is supposed to have multiple entries, then DISTINCT works then! I was just making sure you weren't dealing with multiple entries when there was only supposed to be one.
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”