mysqli的准备语句不能读取的unicode(mysqli prepared statement

2019-10-17 17:21发布

假设Adams' Inn America's Best Inn存储在数据库表中establishment

我要检查Adams' Inn America's Best Inn在变量$EstablishmentName

$stmt = $sql->prepare("SELECT ID FROM `establishment` WHERE Name=? LIMIT 1");
$stmt->bind_param("s",$EstablishmentName);
$stmt->execute();
$stmt->store_result();
print $stmt->num_rows;

问题是我无法找到他们。

输出 0

注意封闭$EstablishmentNamemb_convert_string($EstablishmentName,'HTML-ENTITIES')否则你会拥有错误Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='

更新:我使用set_charset('utf8')为客户端; 表字符集是UTF8,和整理是utf8_general_ci 。 文本编码为原文是什么。 我使用下面的DB经理:SQLyog的和PHPMyAdmin中。

更新#2:我已附上截图给你看,我说的是正确的字符集。

我用$sql->set_charset('utf-8');

Array
(
    [BrandName] => America's Best Value Inn
    [try1] => 1
)

Array
(
    [BrandName] => Adams' Inn
    [try1] => 0
)

Array
(
    [BrandName] => Ambassador Inn and Suites
    [try1] => 1
)

Array
(
    [BrandName] => Amberley Suite Hotel
    [try1] => 1
)

Array
(
    [BrandName] => America's Best Value Inn
    [try1] => 0
)

更新#3好吧,抱歉。 列Namelatin1_swedish_ci
我已经更新了列Name

ALTER TABLE establishment MODIFY NAME VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_general_ci;

这应该工作,而不mb_convert_string()现在。

Answer 1:

只要把

mysqli_set_charset($db_connection, 'utf8');

后您的数据库连接。

$con=mysqli_connect("localhost", "root", "xxxxx","database");
mysqli_set_charset($con, 'utf8');


Answer 2:

我唯一能告诉我在这里看到很多困惑

  • 准备语句是这里无关紧要,因为它们只实现了数据的替代方法,但不改变数据本身。 您将有相同的结果直接增加值转换成数据库。
  • 因为这个错误是由内部内部编码冲突引发的,而不是由数据格式mb_convert_string不能引起排序错误的非法组合。
  • 没有代码来证明存储的值等于一用来检查

假设你可以得到Adams' Inn America's Best Inn根据ID值,我建议你去找回它,然后手动比较。 的var_dump()他们。 第一urlencode()来他们 - 如果仍然无法看到差异



Answer 3:

这解决了这个问题:

iconv('windows-1250', 'utf-8', $EstablishmentName);


文章来源: mysqli prepared statement cannot read unicode