哪个MySQL排序规则与PHP的字符串比较完全匹配? [英] Which MySQL collation exactly matches PHP's string comparison?

查看:101
本文介绍了哪个MySQL排序规则与PHP的字符串比较完全匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个MySQL查询:

Here is a MySQL query:

SELECT last_name FROM users ORDER BY last_name

我们将所有数据导入PHP,然后运行:

We get all that data into PHP and then run:

$prior = NULL;
do {
    $current = array_shift($results);
    assert($current >= $prior);
    $prior = $current;
} while ($current !== NULL);

目前此声明对某些输入失败。是否可以在上面的MySQL查询中添加一个COLLATE子句,以绝对保证PHP断言?

Currently this assertion fails for certain inputs. Is it possible to add a COLLATE clause to the MySQL query above to absolutely guarantee that PHP assertion?


  • 以上代码不适用于某些非ASCII输入

  • ORDER BY电子邮件COLLATE utf8_bin 导致 COLLATION'utf8_bin'无效CHARACTER SET'latin1'

  • 也许这是

  • The above code, it doesn't work for certain non-ASCII inputs
  • ORDER BY email COLLATE utf8_bin resulted in COLLATION 'utf8_bin' is not valid for CHARACTER SET 'latin1'
  • Maybe this is a duplicate of What is the best collation to use for MySQL with PHP? but that seemed more subjective, I am seeking a specific answer to a specific problem

其他说明:

  • If it makes a difference, this column is latin1
  • My PHP PDO wrapper library is https://github.com/fulldecent/thin-pdo

推荐答案

echo ('a' == 'A') ? 'a==A ' : 'a <> A ';
echo ('a' == 'á') ? 'a==á ' : 'a <> á ';

两个回来<> 所以我推断,PHP的行为像 latin1_bin (或 _bin 无论你有什么字符集)。

Both came back <>, so I deduce that PHP acts like latin1_bin (or the _bin of whatever charset you have).

这篇关于哪个MySQL排序规则与PHP的字符串比较完全匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆