Serhyi Vynohradov Serhyi Vynohradov - 1 month ago 5
MySQL Question

Strange MySQL Error. (PHP)

I have a following code:

<?php
include("config.php");
$key = 'blahblah';
$sql = "INSERT INTO softversions SET key='$key'";
$result = mysql_query($sql) or die ($mysql_error());
echo "dude";
?>


This gives me an error:

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='svksskjfvns'' at line 1


The thing is that I've used this script about a hundred times on other pages and it worked.
Table and field names are 100% correct.

I don't understand what is going on.
Do you see the syntax error there?

Answer

key is a reserved word in MySQL. To use it as a column, you need to escape it every time you call it.

$sql = "INSERT INTO softversions SET `key`='$key'";
Comments