View Full Version : Mysql Syntax error PLEASE HELP - SHORT QUESTION

06-12-2011, 03:21 PM
Hi, i have been trying for 3 hours now to get one simple string of information from a mysql database using php. I have used the exact same code before by only changing the mysql command to other database information... i have tryed multiple codes without success.

Mysql returns syntax error like this: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Key FROM pptable WHERE Username=dexter' at line 1

i have two files, includes/connection.php and process.php

Here is the content of both:


// Below lines sets the information to connect to the database... change for use with your own information...
$host = "";
$user = "root";
$password = "";
$database = "database";

$connect = mysql_connect($host, $user, $password) or die (mysql_error());
mysql_select_db($database) or die (mysql_error());


$key ="";
$user = "";
$key = $_GET['key'];
$user = $_GET['user'];

if ($user == ""){
echo "<h3>* Was not able to gather user...<hr></h3>";
if ($key == ""){
echo "<h3>* Was not able to gather key...<h3><br><hr>";

$query = "SELECT Key FROM pptable WHERE Username=$user";
$query = mysql_query($query) or die (mysql_error());
echo $query;

How can i fix this syntax error? ps. The mysql database is correctly configured...

06-12-2011, 03:55 PM
Check http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

KEY is a reserved word, so you can't as such use it as the name of a table/field inside a query. You may use backticks to avoid the error, but using those words as table/field names is a bad practice.

Old Pedant
06-13-2011, 06:00 AM
Also, you almost surely need quotes (apostrophes) around the supplied username.


$query = "SELECT `Key` FROM pptable WHERE Username='$user'";

Be careful: Those are backticks (usually share the keyboard with the ~ key) around Key and apostrophes (share the keyboard with the " key) around $user.