2013-02-16 37 views
1

假設Adams’ InnAmerica’s Best Inn存儲在數據庫表establishmentmysqli的預處理語句不能讀取的unicode

我要檢查Adams’ InnAmerica’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。文本被編碼爲原始文本。我正在使用以下數據庫管理器:SQLyog和PHPMyAdmin。

UPDATE#2:我附上了截圖給你看,我說的是正確的字符集。

screenshot_1

我用$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好了,對不起。 Name列是latin1_swedish_ci
我已經更新了列Name

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

這應該沒有mb_convert_string()現在的工作。

+0

所以你的數據庫連接的字符集是什麼?你餐桌上的字符集?你提到htmlentities ...那麼'''存儲爲編碼實體?原始文本?沒有更多細節,你的問題就無法得到解答。 – 2013-02-16 08:30:19

+0

這兩個文本都被編碼爲原始文本。我爲客戶端提供了set_charset('utf8'),表字符集是utf8,排序規則是utf8_general_ci。 – 2013-02-16 08:31:48

+0

@MarcB你對此有何想法? – 2013-02-16 09:02:10

回答

-2

這解決了這個問題:

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

我唯一可以告訴大家,我看到很多混亂這裏

  • 準備語句是這裏無關緊要,因爲它們只實現了數據的替代方法,但不改變數據本身。您將有相同的結果直接將值添加到數據庫中。
  • mb_convert_string不會導致歸類錯誤的非法混合,因爲此錯誤是由內部內部編碼衝突而不是數據格式觸發的。
  • 沒有代碼,以證明儲值等於一個用來檢查

假設你可以根據ID獲取Adams’ Inn America’s Best Inn價值,讓我建議你去找回它,然後手動比較。 var_dump()他們。如果仍然無法看到差別 - urlencode()來他們第一次

+0

我已經更新了這個問題。列名被設置爲'latin1_swedish_ci',我解決了它,但仍然存在問題。 – 2013-02-16 09:28:25

0

只要把

mysqli_set_charset($db_connection, 'utf8'); 

數據庫連接後。

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