-1

I have a error in my SQL statement. I am using NULL in my command and I guess thats the problem, but I am not sure. So what am I doing wrong here ?

Code:

function run()
    {
        $sql = "UPDATE %%EVENT%% SET lock = NULL WHERE 'lock' IS NOT NULL";

        Database::get()->update($sql);
    }

Error:

USER ERROR: "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'lock = NULL WHERE 'lock' IS NOT NULL' at line 1
Mārcis
  • 79
  • 6
  • Possible duplicate of [SQL #1064 while using phpMyAdmin](https://stackoverflow.com/questions/46354586/sql-1064-while-using-phpmyadmin) – SuRo Jun 06 '19 at 08:41
  • Which kind of SQL are you using? MySQL? PostgreSQL? – Tom Barden Jun 06 '19 at 08:42
  • 2
    Possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](https://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – Qirel Jun 06 '19 at 08:44
  • 1
    `lock` is a reserved keyword in MySQL. You should either use ticks around it, or find a different name for your column. `SET \`lock\` = NULL WHERE \`lock\` IS NOT NULL";` – Qirel Jun 06 '19 at 08:44
  • Thanks the ticks worked for me. I tested them before and it did not work, but now suddently everything works as it should. Thanks :D – Mārcis Jun 06 '19 at 10:29

1 Answers1

0

Try removing the apostrophes around the second 'lock':

UPDATE %%EVENT%% SET lock = NULL WHERE lock IS NOT NULL

Without knowing the SQL dialect you're using it's hard to further diagnose the issue. It's possible that lock is a reserved keyword. What are you trying to achieve with %%EVENT%%? I assume you're trying to use wildcard.

Tom Barden
  • 326
  • 2
  • 13