MySQL 返回所有带有空字符串的行作为 where 子句
我在调试 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是
codeigniter
,请尝试使用以下内容:Try using following, if you are using
codeigniter
: