0

I have codes that players can redeem for in-game goods like currency. These codes are one time codes, once they are used they are inserted into the players MySQL row on my table.

How can I check if a code already exists in a MySQL table's row?

I am not sure how to do this.

<?php

$code = 542345234543; 

$query = "SELECT * FROM myTable WHERE token = '$code'";
$database->query($query);

$database->close();

?>
JavaC3code
  • 159
  • 1
  • 8
  • use the SQL `count()` function – Ardit Meti May 02 '17 at 12:16
  • The above code should work, except that you're currently not checking the response at all. You should also use `count()`, as the above comment suggests. – M. Eriksson May 02 '17 at 12:16
  • i think you have solved your problem. – Rotimi May 02 '17 at 12:26
  • Slightly more efficient to use `SELECT 1 FROM table WHERE ...` rather than `SELECT *` if you're just checking to see if something exists. Assuming you're using `mysqli` then `$database->query()->num_rows()` will either be 1 or 0 depending on whether the record exists or not. – CD001 May 02 '17 at 12:29
  • @CD001 That worked, thanks! Might want to put it as a answer I'll accept it. – JavaC3code May 02 '17 at 12:34

0 Answers0