<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>characterEncoding &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/characterencoding/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Sun, 12 Mar 2023 13:09:45 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>characterEncoding &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Java / PHP 插入 emoji 資料到 MariaDB</title>
		<link>https://noter.tw/122/java-php-%e6%8f%92%e5%85%a5-emoji-%e8%b3%87%e6%96%99%e5%88%b0-mariadb/</link>
					<comments>https://noter.tw/122/java-php-%e6%8f%92%e5%85%a5-emoji-%e8%b3%87%e6%96%99%e5%88%b0-mariadb/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Thu, 28 Jun 2018 14:57:34 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[utf8mb4]]></category>
		<category><![CDATA[useUnicode]]></category>
		<category><![CDATA[characterEncoding]]></category>
		<category><![CDATA[錯誤]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[emoji]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mariadb]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=122</guid>

					<description><![CDATA[<p>Java 跟 PHP 新增 emoji 字串進去 MySQL / MariaDB 資料庫的時候發生錯誤？這篇可以幫助你喔！</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/122/java-php-%e6%8f%92%e5%85%a5-emoji-%e8%b3%87%e6%96%99%e5%88%b0-mariadb/" data-wpel-link="internal">Java / PHP 插入 emoji 資料到 MariaDB</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>這件事是這樣的，小蛙需要接收一個從 App 過來的表單資料，就在接收後要塞進 MariaDB 的時候，問題就出現了 &#8230;</p>
<p><span id="more-122"></span></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">Error Code: 1366. Incorrect string value: '\xF0\x9F\x92\xA2' for column 'SOURCE_CONTEXT' at row 1</pre>
<p>一再檢查要塞入的字串感覺沒有什麼問題，試了好幾次之後才發現 &#8230; 原來裡面包含了 emoji 啦！！！</p>
<p>之前跟同事聊天的時候得知，如果要存 emoji 的話要把資料庫的欄位改成 utf8mb4，但是呢 &#8230; 小蛙實在不想動太多，參考資料裡有很多解法，有興趣可以去看看。</p>
<p>小蛙這邊只是把要存 emoji 的欄位改成 utf8mb4，然後 &#8230; php 一執行，還是噴一樣的錯誤出來，嗯 ~ 這個案情不單純，來慢慢 try ~</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">jdbc:mysql://ip:port/db_name?useUnicode=true&amp;characterEncoding=utf8mb4</pre>
<p>試了一些方法之後發現，在 php 的部份連線後，在做任何操作之前先執行</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">mysqli_query( $conn, "SET NAMES 'utf8mb4'");</pre>
<p>ㄟ ~ 竟然好像貌似就可以了！另一個專案需要用 Java 去剖析並塞入有 emoji 的需求，立馬試試</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">ps.executeQuery("SET NAMES 'utf8mb4'");</pre>
<p>哦哦哦哦哦哦哦哦哦哦哦哦哦 ~</p>
<p>有很多文章的教學是根本的解決方法，像是把資料庫各項設定都直接改成 utf8mb4，小蛙不想動到太多，試出這方法，最後總結一下：</p>
<ol class="my-li bg-darkblue">
<li>mysql connector 更新到新版</li>
<li>jdbc:mysql://ip:port/db_name?useUnicode=true&amp;characterEncoding=utf-8&amp;character_set_server=utf8mb4</li>
<li>建立 Statement 或 PreparedStatement 後就立馬執行 SET NAMES &#8216;utf8mb4&#8217;</li>
</ol>
<p>以上，希望幫助到有需要的人！</p>
<h2 class="para">參考資料</h2>
<ol class="my-li bg-darkblue">
<li><a href="https://stackoverflow.com/questions/44591895/utf8mb4-in-mysql-workbench-and-jdbc" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">utf8mb4 in MySQL Workbench and JDBC &#8211; Stack Overflow<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li>
<li><a href="https://stackoverflow.com/questions/46140583/caused-by-java-sql-sqlexception-unsupported-character-encoding-utf8mb4" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">Caused by: java.sql.SQLException: Unsupported character encoding &#8216;utf8mb4&#8217; &#8211; Stack Overflow<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li>
<li><a href="https://stackoverflow.com/questions/47060827/jdbc-and-utf8mb4-encoding-the-emoji-symbol" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">JDBC and utf8mb4 encoding the emoji symbol ? &#8211; Stack Overflow<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li>
<li><a href="https://blog.csdn.net/sunny05296/article/details/78488366" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">mysql/Java服务端对emoji(utf8mb4编码)的支持有关的问题 &#8211; CSDN 博客<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li>
</ol>


<p><strong>MySQL / MariaDB / Oracle 相關文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list">
<li><a href="https://noter.tw/12503/mariadb-json_extract-%e5%be%9e-json-%e6%ac%84%e4%bd%8d%e5%8f%96%e5%80%bc/" data-wpel-link="internal">MariaDB JSON_EXTRACT: 從 JSON 欄位取值</a></li>



<li><a href="https://noter.tw/12015/oracle-%e5%88%86%e7%b5%84%e7%b7%a8%e8%99%9f%e8%88%87%e5%85%a8%e6%8e%92%e5%ba%8f%e7%b7%a8%e8%99%9f-row_number-over-partition-by/" data-wpel-link="internal">Oracle, MySQL 分組編號與全排序編號 row_number, over, partition by</a></li>



<li><a href="https://noter.tw/3929/mysql-error-2003-hy000-%E7%84%A1%E6%B3%95%E9%81%A0%E7%AB%AF%E9%80%A3%E7%B7%9A/" data-wpel-link="internal">MySQL ERROR 2003 (HY000) 無法遠端連線</a></li>



<li><a href="https://noter.tw/2889/jsp-%E9%80%A3%E7%B5%90-mysql/" data-wpel-link="internal">JSP 連結 MySQL</a></li>



<li><a href="https://noter.tw/2883/java-%E9%80%A3%E7%B5%90-mysql/" data-wpel-link="internal">Java 連結 MySQL</a></li>



<li><a href="https://noter.tw/2306/mysql%E6%8E%92%E7%A8%8Bevent-scheduler/" data-wpel-link="internal">MySQL 排程(Event Scheduler)</a></li>



<li><a href="https://noter.tw/1988/java%E9%80%A3%E7%B5%90%E9%81%A0%E7%AB%AFmysql/" data-wpel-link="internal">Java 連結遠端 MySQL</a></li>



<li><a href="https://noter.tw/4508/mariadb-replication-%E8%A8%AD%E5%AE%9A/" data-wpel-link="internal">MariaDB Replication 設定</a></li>



<li><a href="https://noter.tw/122/java-php-%E6%8F%92%E5%85%A5-emoji-%E8%B3%87%E6%96%99%E5%88%B0-mariadb/" data-wpel-link="internal">Java / PHP 插入 emoji 資料到 MariaDB</a></li>
</ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/122/java-php-%e6%8f%92%e5%85%a5-emoji-%e8%b3%87%e6%96%99%e5%88%b0-mariadb/" data-wpel-link="internal">Java / PHP 插入 emoji 資料到 MariaDB</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/122/java-php-%e6%8f%92%e5%85%a5-emoji-%e8%b3%87%e6%96%99%e5%88%b0-mariadb/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
