ChrisBoyd ChrisBoyd - 1 month ago 8
SQL Question

How do I check if data in a column already exists and to check the second column of that row to equal a specific number


I am having trouble with creating a MySQL statement.


I have one column named 'keys', and another column named 'isUsed', and the 'keys' column consists of many different keys in this format: xxxx-xxxx-xxxx, and I want to check if that key has already been used or not.

This is the format of the table:

Key | isUsed
xxxx-xxxx-xxxx | 0

I have already made it so it checks if the key exists by using this code:

$arrExistKey = mysqli_query($resDBCon, "SELECT Key FROM keys WHERE Key = '$strKey'");
$intKeys = mysqli_num_rows($arrExistKey);

if ($intKeys != 1) {
sendError('The beta-key does not exist!');

So I have checked if the key exists, now I'm just trying to figure out how to check if it has already been used.

If anyone can help me out, that would be great!
Thank you.

Answer Source

How about just encoding the logic in the SQL?

$arrExistKey = mysqli_query($resDBCon, "SELECT Key FROM keys WHERE Key = '$strKey' AND isused = 1");