Page 1 of 1
While Statement to only run once for an ID
Posted: Wed Jan 11, 2012 12:19 am
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))
{
}
}
}
}
Re: While Statement to only run once for an ID
Posted: Wed Jan 11, 2012 5:41 am
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.
Re: While Statement to only run once for an ID
Posted: Wed Jan 11, 2012 6:41 am
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'";
Re: While Statement to only run once for an ID
Posted: Wed Jan 11, 2012 2:44 pm
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.
Re: While Statement to only run once for an ID
Posted: Thu Jan 12, 2012 1:19 am
by Jackolantern
Were you searching for PHP solutions or MySQL solutions? If you were looking for PHP solutions, that is probably why.
Re: While Statement to only run once for an ID
Posted: Thu Jan 12, 2012 1:59 am
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'";
Re: While Statement to only run once for an ID
Posted: Thu Jan 12, 2012 3:38 am
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.
Re: While Statement to only run once for an ID
Posted: Thu Jan 12, 2012 1:07 pm
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.
Re: While Statement to only run once for an ID
Posted: Thu Jan 12, 2012 8:57 pm
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.