0

I have this little bit of code here and I am totally lost as I am getting a syntax error near 'unique = 'dD0231q' LIMIT 1' at line 1. It might be something very simple, but I seem to be blind to my own errors..

 $unique = $_GET["unique"];
 $results = $mysqli->query("SELECT * FROM written WHERE unique = '$unique' LIMIT 1
Ivan Venediktov
  • 426
  • 2
  • 15

2 Answers2

3

UNIQUE is a keyword in MySQL. If you want to use it as a table column name please wrap it in ` (backtick) quotes like this:

SELECT * FROM written WHERE `unique` = '$unique' LIMIT 1

Also please don't just put values you recieve from a user directly into your query. That's how SQL injections happen. Rather use Prepared Statements.

Tom
  • 3,031
  • 1
  • 25
  • 33
2

UNIQUE is a MySQL reserved word http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

either wrap the column in ticks or rename it.

SELECT * FROM written WHERE `unique` = '$unique'

The error says it all:

syntax error near 'unique


Plus, in regards to SQL injection which is something you are open to, use mysqli with prepared statements, or PDO with prepared statements, they're much safer.


Just for argument's sake, finish it off:

$results = $mysqli->query("SELECT * FROM written WHERE `unique` = '$unique' LIMIT 1");
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141