MySQL 返回所有带有空字符串的行作为 where 子句

发布于 2024-12-08 06:05:02 字数 749 浏览 2 评论 0原文

我在调试 CodeIgniter 活动记录时发现了这个问题,如下所示:

$this->db->from("table_name");
$this->db->where("field_name", "");
$result = $this->db->get()->result_array();

结果查询是:

SELECT * FROM `table_name` WHERE `field_name` = 0;  // Returns all rows in table

即使空字符串被转换为 0,我们也期望从 table_name.field_name 充满非空字符串值。但是,我从这个查询中获取了整个表。有人明白为什么吗?这根本不直观。

我尝试了没有强制转换为 0 的查询,并且它有效:

SELECT * FROM `table_name` WHERE `field_name` = "";  // Empty result

为什么强制转换为 0?


编辑:使用此替代 CodeIgniter 语法会发生相同的 0 转换:

$this->db->query('SELECT * FROM table_name WHERE field_name = ?', array(""));

I discovered this problem while debugging CodeIgniter active record as shown below:

$this->db->from("table_name");
$this->db->where("field_name", "");
$result = $this->db->get()->result_array();

The resulting query is:

SELECT * FROM `table_name` WHERE `field_name` = 0;  // Returns all rows in table

Even though the empty string is cast to 0, we expect an empty result since table_name.field_name is full of non-empty string values. However, I get the entire table from this query. Anyone understand why? This is not intuitive at all.

I tried the query without the cast to 0 and it works:

SELECT * FROM `table_name` WHERE `field_name` = "";  // Empty result

Why the cast to 0?


EDIT: The same cast to 0 happens with this alternative CodeIgniter syntax:

$this->db->query('SELECT * FROM table_name WHERE field_name = ?', array(""));

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

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

发布评论

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

评论(1

嘿嘿嘿 2024-12-15 06:05:02

如果您使用的是 codeigniter,请尝试使用以下内容:

$this->db->where("field_name = ''");

Try using following, if you are using codeigniter:

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