MySQL 更新返回失败

发布于 2025-01-12 17:44:44 字数 591 浏览 1 评论 0原文

我正在运行 MySQL 更新查询,但它总是失败。我正在使用 WordPress,我还尝试从 phpMyAdmin 运行相同的查询,但也失败了。

这是我的表:

idperiodxy
2021 年 9 月1 日- 2021 年 11 月

我尝试运行 get count:

SELECT COUNT(*) FROM `utility_period` WHERE 'period' = 'Sep 2021 - Nov 2021'

当记录存在于表中时,这会返回 0。

更新查询:

UPDATE `utility_period` SET total = '0’ WHERE `period` = ‘Sep 2021 – Nov 2021’

为什么会发生这种情况?

I am running a MySQL update query, but it always fails. I am using WordPress, I also tried to run the same query from phpMyAdmin which also fails.

This is my table:

idperiodxy
1Sep 2021 - Nov 2021

I tried running get count:

SELECT COUNT(*) FROM `utility_period` WHERE 'period' = 'Sep 2021 - Nov 2021'

This returns 0 while the record exists in the table.

UPDATE Query:

UPDATE `utility_period` SET total = '0’ WHERE `period` = ‘Sep 2021 – Nov 2021’

Why is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦巷 2025-01-19 17:44:44

tl;dr:' 不是 ''。它们很难区分,但作为程序员你必须区分。

在 MySQL 中,字符串文字用单引号 ' 括起来。

在此查询中,您被一个“有用”的文字处理程序欺骗了,该程序用 '' 替换了这些单引号。它们很难区分,但是 '' 在 SQL(或大多数编程语言)中不能用作字符串封闭标记。

并且,数据库、表和列的名称可以选择用反引号括起来,就像查询一样。仅当对象名称也是 SQL 中的关键字时才需要反引号。

所以

 UPDATE `utility_period` SET total = '0’ WHERE `period` = ‘Sep 2021 – Nov 2021’

应该是这样

 UPDATE `utility_period` SET `total` = '0' WHERE `period` = 'Sep 2021 – Nov 2021'

或这样

 UPDATE utility_period SET total = '0' WHERE period = 'Sep 2021 – Nov 2021'

SQL 短语 WHERE 'period' = 'Sep 2021 - Nov 2021' 确定文本字符串常量 'period' 是否等于常量“2021 年 9 月 - 2021 年 11 月”。它不是。您在该列名称周围使用了 ' 而不是反引号。

使用 $wpdb 意味着您将处理字符串文字的责任放在该模块上。它做得正确。

tl;dr: ' is not or . They're hard to tell apart, but as a programmer you must.

In MySQL, string literals are enclosed in single-quotes ' .

In this query you got skunked by a "helpful" word processing program that replaced those single quotes with and . They're very hard to tell apart, but and are not useful as string-enclosing marks in SQL (or in most programming languages).

And, the names of databases, tables, and columns are optionally enclosed in backticks, as your query does. The backticks are only required when an object name is also a keyword in SQL.

So this

 UPDATE `utility_period` SET total = '0’ WHERE `period` = ‘Sep 2021 – Nov 2021’

should be this

 UPDATE `utility_period` SET `total` = '0' WHERE `period` = 'Sep 2021 – Nov 2021'

or this

 UPDATE utility_period SET total = '0' WHERE period = 'Sep 2021 – Nov 2021'

The SQL phrase WHERE 'period' = 'Sep 2021 - Nov 2021' determines whether the text string constant 'period' is equal to the constant 'Sep 2021 - Nov 2021'. It is not. You used ' around that column name rather than backticks.

Your use of $wpdb means you put the responsibility for handling your string literals on that module. It does it correctly.

面犯桃花 2025-01-19 17:44:44

谢谢大家。现在正在工作。
我无法识别问题,但现在再次使用 wpdb,问题得到了解决。

         $replace = [
             "total" => $new_total
           ];
  
        $where = ["period" => $period];
  
        $update = $wpdb->update("utility_period", $replace, $where);

Thanks, everyone. It's working now.
I was not able to identify the issue, but now again used wpdb and the issue was resolved.

         $replace = [
             "total" => $new_total
           ];
  
        $where = ["period" => $period];
  
        $update = $wpdb->update("utility_period", $replace, $where);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文