问题
I'm trying to delete a row from my mySQL database via PHP and it isn't working. I have tried the following:
mysql_query("DELETE FROM table WHERE id='$id'");
mysql_query("DELETE FROM 'table' WHERE id='$id'");
mysql_query("DELETE FROM `table` WHERE id=`$id`");
$id is the unique identifier and I've echoed it to make sure it's coming across. Using "or die" results in death. The row simply won't go away.
Any other syntax I should try?
回答1:
I understand that you were trying to try every possible combination, but it helps to know what the specific punctuations actually mean in the MySQL syntax.
`structure`
Grave accents are used only around structures like column names, etc.
'value'
Apostrophes are used for the values, as in id='$id'
.
mysql_query("DELETE FROM table WHERE id='$id'");
This is valid syntax. You can also put grave accents around table
.
mysql_query("DELETE FROM 'table' WHERE id='$id'");
This is invalid syntax. 'table'
is invalid, since it's a column not a value.
mysql_query("DELETE FROM `table` WHERE id=`$id`");
This is invalid syntax. You put grave accents around the value, when it should have been apostrophes.
Hopefully those explanations helps you figure out your issues.
回答2:
The error checking helped me discover that I had a mysql_close();
before my second mysql_query
. My syntax was correct, I just didn't check the remainder of the code carefully enough.
回答3:
Is your id
integer? Try
mysql_query("DELETE FROM `table` WHERE id=$id");
回答4:
Have you tried:
mysql_query("DELETE FROM table WHERE id=".$id);
I ask because you may be quoting a number (your id) - and you don't need (or want) to do that.
Let me know if this works - I'm no expert but it's my first glimpse at it.
来源:https://stackoverflow.com/questions/11919217/mysql-querydelete-from-table-where-id-id-fails