简体   繁体   English

更新SQL数据库中的BIT字段时出错(使用PHP PDO驱动程序)

[英]Error updating BIT field in SQL database (using PHP PDO driver)

I have the following code: 我有以下代码:

$SQL = "UPDATE jobs
        SET read = '1'
        WHERE id = '$job_id'";

$STH = $DBH->prepare($SQL);
$STH->execute();

read is a field in the table with a data type of BIT . read是表中具有BIT数据类型的字段。 The current data inside this field is 0 (false), and I'm trying to change it to 1 (true). 该字段中的当前数据为0 (假),而我试图将其更改为1 (真)。 However, I am getting this error: 但是,我收到此错误:

Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 消息:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read = '1' WHERE id = '25'' at line 2 检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在第2行的'read ='1'WHERE id ='25''附近使用

I can't see any syntax errors, so what is the problem? 我看不到任何语法错误,这是什么问题?

READ is a reserved word in MySQL. READ是MySQL中的保留字

You will need to quote it in order to use it as column name: 您需要引用它才能将其用作列名:

$SQL = "UPDATE jobs
        SET `read` = '1'
        WHERE id = '$job_id'";

This is why a quote everything in every query, to avoid problems like this. 这就是为什么在每个查询中都用引号引起来的原因,以避免出现此类问题。 Remember, quoting object names (tables, columns etc) must be done with backticks : ` 请记住,引用对象的名称(表,列等)必须使用反引号来完成: `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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