简体   繁体   中英

MySQL Syntax error, using PHP

Thanks in advance for any help I get!

I'm writing a script in PHP to delete all users with (by this point) an already set expiration date. I'm getting a MySQL syntax error, and I can't figure out what I'm doing wrong.

ERROR:

Query failed: 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 ''tablename' WHERE date='01' at line 1

CODE:

$query="DELETE FROM 'tablename' WHERE date='$exdate'";

I did an echo just to test the variable was being passed properly, and it is.

EDIT: "dbname" changed to "tablename" just to clarify, my mistake.

SOLVED: Thanks again for the help, I wasn't using the proper "quotation mark syntax". Thanks!

不要在表名上使用单引号('),而应使用(`)(与键盘上[〜]的小写相同)。

You may need tick marks around date :

$query="DELETE FROM 'dbname' WHERE `date`='$exdate'";

However, from your error, it appears the syntax is not correct earlier. If you are doing some sort of escaping, that may be causing this error.

date是保留关键字,您不能在查询中使用它,请使用date

Don't single quote your table name, if anything use magic quotes (`)

You should not be building the query by concatenating strings.

To avoid potential issues with SQL injection, it would be wise to use prepared queries.

I've just confirmed this on a mysql server

mysql> delete from 'sessions' where `last_activity` = 0;
ERROR 1064 (42000): 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 ''sessions' where `last_activity` = 0' at line 1
mysql> delete from sessions where `last_activity` = 0;
Query OK, 0 rows affected (0.00 sec)

Simplest correction

$query="DELETE FROM tablename WHERE date='$exdate'";

Best (safest) version is

$query="DELETE FROM `tablename` WHERE `date`='$exdate'";

Explanation :

tableNames, columnnames etc are enclosed in quotes like `name` instead of 'name'
Enclosing them in `` is optional usually, but if your tablename,columnane is some
what special word e.g 'table' then you can use `table` and not simply table

Here you can use

`tablename` (best) tablename (simplest) but not 'tablename'(worng)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM