CakePHP - 如果查询中有特殊字符,则查询返回空字段
我认为 CakePHP 中有一个正常的查询调用 - 它适用于所有结果,但是当字段中有特殊字符时,该字段将返回空。它不会破坏 - 而且它仍然给我其余的字段 - 只是那一个字段是空的。
示例:
$this->paginate = array(
'conditions' => array(
'Item.name != ' => '',
),
);
$data = $this->paginate('Item');
这将返回表中的所有项目(包括我认为名称字段为空的项目) - 但是当我尝试将名称回显到页面时,它适用于除具有特殊字符的项目之外的每个项目( é)。我把它改成了普通的“e”,它显示得很好。
即使名称中包含特殊字符,我如何返回结果?提前致谢!
I have what I think to be a normal query-call in CakePHP - it works for all results, but when a field has a special character in it, the field will return empty. It doesn't break - and it still gives me the rest of the fields - it's just that one field that's empty.
Example:
$this->paginate = array(
'conditions' => array(
'Item.name != ' => '',
),
);
$data = $this->paginate('Item');
This will return all the items in my table (including the one that I thought had an empty name field) - but when I try to echo the name to the page, it works for every item except the one with a special character (é). I changed it to a normal "e" and it shows up fine.
How can I return results even if they have a special character in their name? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查以确保您的数据库使用正确的编码(最好是 UTF-8),并且您已将 Cake 配置为在 config/database.php 中也使用相同的编码:
如果您有一些编码不匹配,您的应用程序可能已经在数据库中存储了垃圾,因此请确保使用正确的数据和/或新数据库进行测试。
Check to make sure your database uses the right encoding (UTF-8, ideally) and you have configured Cake to use this same encoding as well in
config/database.php
:If you have some encoding mismatch, your app has probably stored garbage in the database already, so make sure you test with the right data and/or a fresh database.
这可能不是 Cake 特定的问题,而是 PHP / MySQL 的问题。 (其他人已经提出了编码,所以我将跳过它。)
单引号意味着一个文字字符串被传递给 MySQL:
'Item.name != ' =>; ''
PHP(a la Cake)可能会按字面意思解析该字符串。事实上,它甚至可能像这样解析它:(
注意操作数后面没有任何内容?如果它在 SQL 查询中落在最后,查询不会出错,它可能仍然有效!)
当你打算测试它时:(
注意字符串中现在包含空单引号)
但是,由于您没有收到错误 - 并且其余数据会拉出! - 您可能想要编辑该语句,因为您的问题更可能是语法。
尝试一下。
http://dev.mysql.com/doc/refman/5.6 /en/comparison-operators.html 描述 IS NOT NULL 与 IS NOT (bool) 与 <>和 !=(不等于)。
哈:)
This is probably less of a Cake-specific issue and more of a PHP / MySQL issue. (Others have already brought up encoding, so I'll skip that.)
Single quotes means a literal string being handed to MySQL:
'Item.name != ' => ''
PHP (a la Cake) probably parses that string quite literally. In fact, it's might even be parsing it like:
(note there's nothing after the operand? and if it falls last in the SQL query, the query wouldn't error, it would probably still work!)
When you meant for it to test:
(note empty single quotes now included in the string)
However, since you're not getting an error - and the rest of the data pulls! - you probably want to edit that statement, because your problem is more likely syntax.
Give those a try.
http://dev.mysql.com/doc/refman/5.6/en/comparison-operators.html describing IS NOT NULL vs IS NOT (bool) vs <> and != (is not equal to).
HTH :)