0

Hi guys this is my first post here, there have been similar questions on this forum but I couldn't find one which accurately answered my question or at least it wasn't relevant enough for me to workout an answer - this is my 3rd day using PHP so I am still getting used to it..

I am making a small game for my site where by users can pay a small amount of money to win a prize, the prize is a code which will be emailed to them.

Currently I have my database and script setup so that the same code cannot be issued twice using the boolean Available which is set to 1 if the code has not been issued and 0 if it has

I have my code table setup like so

CodeID | ItemID | CodeType | Available | Code             |

1      | 1      | xxxxxxx  | 1         | QLLxbttKQcHv4e4k |
2      | 1      | xxxxxxx  | 1         | 6JKMumiH35Dke4SS |
3      | 3      | xxxxxxx  | 0         | mQ2WyUjqNy5fJiEr |

What I want to do is check each code to see if it is available, if it is I want to take the ItemID and put it in a $_SESSIONS array which I can use in another page to only return results for codes that I have stocked

I am having some trouble checking if the code is available and if it is putting the ItemID in the $_SESSIONS array

Currently I have this to query the database

$stock = $conn -> query('SELECT * FROM codes WHERE Available = 1');

This checks the database and out puts all codes which are currently available, including duplicate ItemIDs

Can anyone help me so that I only take 1 of each ItemID and put that into the $_SESSIONS array which I can use on the next page to only show results with available codes?

2
  • Why don't you perform the query on the next page so that you don't need to use Session at all? Commented Mar 13, 2013 at 22:04
  • It seems to me you are doing a mess, perhaps if you make it clearer what you need we can give you a better solution. Commented Mar 13, 2013 at 22:05

1 Answer 1

1

This query

SELECT DISTINCT ItemID FROM codes WHERE Available = 1

will return you only unique ItemIDs that are available

PS: prefer to select only particular columns you need, not everything with *

Sign up to request clarification or add additional context in comments.

1 Comment

Oh so simple, thanks a lot! I had set up the query with the values I wanted but to make it look a bit cleaner I used * in future I'll stick to only values I need, cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.