CI架構下 Mysql存入漢字出現編碼問題

來源:互聯網
上載者:User

在CI架構下存入漢字 報資料庫錯誤

這是代碼:$comData = array( 'textArea' => $textArea, 'commentID' => $newComID, 'postID' => $newPostID, );$this->db->insert('COMMENTMETA',$comData);下面是報錯資訊:Error Number: 1366

Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95' for column 'textArea' at row 1

INSERT INTO COMMENTMETA (textArea, commentID, postID) VALUES ('測試', 8, 28)

Filename: F:\code\CI\system\database\DB_driver.php

Line Number: 330

資料庫編碼:Mysql的編碼都是utf8

回複內容:

在CI架構下存入漢字 報資料庫錯誤

這是代碼:$comData = array( 'textArea' => $textArea, 'commentID' => $newComID, 'postID' => $newPostID, );$this->db->insert('COMMENTMETA',$comData);下面是報錯資訊:Error Number: 1366

Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95' for column 'textArea' at row 1

INSERT INTO COMMENTMETA (textArea, commentID, postID) VALUES ('測試', 8, 28)

Filename: F:\code\CI\system\database\DB_driver.php

Line Number: 330

資料庫編碼:Mysql的編碼都是utf8

看下錶是否為utf-8,這是在建立表的時候可選的屬性。

update:

我仔細看了一下,你的model看起來是不符合CI規範的,使用了數組。正確的應該是:

$this->[鍵名] = $this->input->post('***');

另外你可以看看mysql的data檔案夾裡面的對應庫的檔案夾裡面的設定檔,那個必須是utf-8.

你的只說明了你的mysql環境是utf8的,不能代表你插入的那個表以及那些欄位也是utf8編碼的,此外,檢查下你檔案的編碼(或者說插入的資料的編碼)是否是utf8

iconv()轉換一下插入字串的編碼

見資料庫相關CI手冊

檢查這一行$db['default']['char_set'] = "utf8";

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.