سؤال

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

هل كانت مفيدة؟

المحلول

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')";

نصائح أخرى

Your problem is because you have an ' in the work you're. So your string is terminating to early in your sentence. Use you\'re instead to escape the character '

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

My experience with MySQL is limited, but I use SQL Server extensively. To me it seems that the problem is in the apostrophy used in the "you're right behind me". In SQL server, I'd have to use a double apostrophy, so the sql instruction would be something like this (notice the double apostrophy in the you''re):

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

Hope this helps. Regards

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top