iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 2
0
自我挑戰組

Experience of a backend novice系列 第 2

如何正確的導入中文到MySQL資料庫而不會出現亂碼?

Day two
How to import Chinese into Database?
如何正確的導入中文而不會出現亂碼?

Day two
Some detail when importing Chinese from CSV file into database via PHP script
如何正確的導入中文而不會出現亂碼?

Hello everyone. It’s Ray!
On the second day of Ironman Challenge, I am going to share more details of importing Chinese characters into database from CSV file via PHP script
大家好,我是Ray!
在挑戰賽的第二天,我想跟大家分享CSV檔案匯入MySQL的更多細節部分,像是如何正確的導入中文字而不會出現亂碼。

Firstly, let’s start from PHP script
首先,先講PHP的部分:

<?php
mysqli_set_charset($dbc,"utf8");

After the code of connecting to database, bear in mind that the code above should be added in order to specify the default format of data from and to database.
在連接資料庫之後,請記得一定要加入上面的code,目的是明確來往資料庫的資料編碼格式。

As to CSV file:
檔案部分:

Firstly, open it with Excel, open new
首先,打開Excel,然後開啓新檔案

Secondly, choose data, and click from text
接下來,點選Data,並且選取From text

Use delimiter
這邊請選擇使用分界符號

Use comma to delimit the data
這裏選擇使用逗號來做分隔

Finally, general is fine.
最後選擇一般即可

As to database:
接下來爲,資料庫部分:

If you use GUI tool such as Sequel Pro, remember to choose UTT-8 when creating a new table
如果你是使用Sequel Pro,那請務必在創建表格時點選UTF-8,如下圖

If you use terminal, as image below, remember to specify the utf8 format when creating a new table
如果你是使用終端機部分,如下圖,請記得要在創立表格的同時賦予utf8的編碼。

If garble still occurs, check if the format for each column is utf-8
如果依然在匯入之後顯示亂碼,請確認column的編碼是否爲utf-8

Basically, if all above mentioned is followed, you should be able to import Chinese and show it in database without any trouble as image below:
基本上如果以上的細節都有注意到,應該就可以順利的導入中文,並且成功的在資料庫內顯示中文,如下圖:

It’s my sharing on the second day. Good luck to all of you!
以上爲挑戰賽第二天的分享,祝大家寫程式愉快~


上一篇
將CSV檔案經由PHP導入MySQL資料庫
下一篇
使用MySQL- group by 來整理資料庫
系列文
Experience of a backend novice30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言