search
HomeBackend DevelopmentPHP Tutorialmysq, php writes unicode characters

<span><span>一些特殊字符</span><span>(</span><span>图标字符</span><span>)</span><span>在保存</span><span>mysql</span><span>时</span><span>,</span><span>不能插入数据库
</span><span>可以先把字符</span><span>(</span><span>特殊字符和正常字符</span><span>)</span><span>用</span><span>base64_encode</span><span>转为</span><span>base64</span><span>编码</span><span>,</span><span>保存到</span><span>mysql
</span><span>取出还原时</span><span>,</span><span>用</span><span>base64_decode</span><span>进行</span><span>base64</span><span>解码</span><span>,</span><span>再用</span><span>json_decode</span><span>还原为原字符</span></span>

Compiled with reference to online information

/**
 * 一些特殊字符(图标字符)在保存mysql时,不能插入数据库
 * 可以先把字符(特殊字符和正常字符)用base64_encode转为base64编码,保存到mysql
 * 取出还原时,用base64_decode进行base64解码,再用json_decode还原为原字符
 */

/**
 * unicode解码
 * @param $str
 * @return mixed|string
 */
function unicode_decode($str)
{
    if (!$str)
        return $str;
    $decode = json_decode($str);
    if ($decode)
        return $decode;
    $str = '["' . $str . '"]';

    $decode = json_decode($str);
    if (count($decode) == 1) {
        return $decode[0];
    }
    return $str;
}

//$re = unicode_decode('\u963f\u55b5\ud83d\udc31\ud83d\udca6');
//echo($re);

/**
 * unicode编码
 * @param $str
 * @return string
 */
function unicode_encode($str)
{
    if (!$str)
        return $str;
    $decode = json_encode($str);
    if ($decode)
        return $decode;
    $str = '["' . $str . '"]';

    $decode = json_encode($str);
    if (count($decode) == 1) {
        return $decode[0];
    }
    return $str;
}

//$re1 = unicode_encode('阿喵');
//echo($re1);


///////////////////////////////////////////////////////////////


/**
 * 对unicode字符进行base64编码,保存入库
 */
$conn = @mysql_connect("localhost","root","111111");
if (!$conn){
    die("连接数据库失败:" . mysql_error());
}

mysql_select_db("wx_sys", $conn);
mysql_query("set character set 'gbk'");   //避免中文乱码字符转换
mysql_query("set character set 'utf8'");   // PHP 文件为 utf-8 格式时使用
//mysql_query("set names 'utf8'");	//PHP 文件为 utf-8 格式时使用

//unicode码字符
$name = '\u963f\u55b5\ud83d\udc31\ud83d\udca6';
//正常中文
//$name = '张三';
//base64编码
$re = base64_encode($name);

$sql = "insert into test (uni) values ('" . $re . "')";

if(!mysql_query($sql,$conn)){
    echo "添加数据失败:".mysql_error();
} else {
    echo "添加数据成功!";
}

/////////////////////
/**
 * 取出数据,base64解码,并还原字符
 */
$sql2 = "select * from test";
$result = mysql_query($sql2)
or die("无效查询: " . mysql_error());

//循环从数据集取出数据
while( $row = mysql_fetch_array($result) ){
    $sr = base64_decode($row['uni']);
    $ab = unicode_decode($sr);
    echo "uni:".$ab."<br>";
}

The above introduces how to write unicode characters in mysq and php, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Oracle DECODE函数的高级用法及技巧分享Oracle DECODE函数的高级用法及技巧分享Mar 08, 2024 am 10:30 AM

Oracle数据库中的DECODE函数是一个非常常用的函数,它可以根据一个表达式的结果值在一组值中进行选择。DECODE函数的语法如下:DECODE(expression,search_value1,result1,search_value2,result2,...,default_result)其中,expression是要进行比较的表达式,s

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

Oracle DECODE函数详解及用法示例Oracle DECODE函数详解及用法示例Mar 08, 2024 pm 03:51 PM

Oracle中的DECODE函数是一种条件表达式,常用于在查询语句中根据不同的条件返回不同的结果。本文将详细介绍DECODE函数的语法、用法和示例代码。一、DECODE函数语法DECODE(expr,search1,result1[,search2,result2,...,default])expr:要进行比较的表达式或字段。search1,

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql需要commit吗mysql需要commit吗Apr 27, 2022 pm 07:04 PM

在mysql中,是否需要commit取决于存储引擎:1、若是不支持事务的存储引擎,如myisam,则不需要使用commit;2、若是支持事务的存储引擎,如innodb,则需要知道事务是否自动提交,因此需要使用commit。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)