Search code examples
phpmysqlreserved-wordsreserved

Sql with mysql reserved word


I have this following sql code:

$sql = "INSERT INTO data (Artist, Name) VALUES ('TF2', 'you're right behind me')";

The code itself looks normal but for some reason mysql doesn't want to allow me to save it. I get the following error:

"#1064 - 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 're right behind me')' at line 1"

What I know the problem is because of the word "right" being reserved in mysql but I need to save it so how should my code look like. All help is appreciated


Solution

  • As @Fred and @JunM have already commented, you have two issues. The first is that Name is a reserved word. The second is that you have a single quote inside your single quoted string. Change your SQL to this:

    $sql = "INSERT INTO data (`Artist`, `Name`) VALUES ('TF2', 'you\'re right behind me')";