Coderrrr
Coderrrr

Reputation: 230

PHP - Check if data is in a table if so pass it to $_SESSIONS array

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?

Upvotes: 0

Views: 124

Answers (1)

zerkms
zerkms

Reputation: 255155

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 *

Upvotes: 1

Related Questions