gpt4 book ai didi

php - 在数据库中插入 Unicode 字符

转载 作者:行者123 更新时间:2023-11-29 14:18:49 26 4
gpt4 key购买 nike

我试图在数据库中插入像 : ( 😄 ) 这样的 Unicode 字符,但是当我在文本区域中插入 ( 😄 ) 时,它会添加一个空字段?!

那么我怎样才能在数据库中添加这样的字符😄😊😃☺😉😍😘😚😳😌😁😜😝?

我的数据库是:

CREATE TABLE `t1` (
`id` int(4) NOT NULL auto_increment,
`msg` varchar(500) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

insert.php 添加数据:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>my page</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="insert-ok.php">
<p>
<label for="textarea"></label>
<textarea name="textarea" id="textarea" cols="45" rows="5"></textarea>
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>
</body>

insert-ok.php 用于插入和查看数据:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>my data</title>
</head>

<body>
<?php
$con = mysql_connect("localhost","root","4530");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_query("set names 'utf8'");
mysql_select_db("test", $con);
$post= $_POST['textarea'];
$sql = "insert into t1 values('$id','$post')";
mysql_query($sql,$con);

$result=mysql_query("select * from t1 ");

while($row = mysql_fetch_array($result))
{

echo $row['msg'].'<br>';
}
?>
</body>
</html>

最佳答案

😄 U+1F604 SMILING FACE WITH OPEN MOUTH AND SMILING EYES

请注意,它超出了 U+FFFF,这意味着它需要 4 字节来存储。

确保您使用的是 MySQL 5.5 或更高版本,并且:

CREATE TABLE `t1` (
...
) ENGINE=MyISAM DEFAULT CHARSET=utf8_mb4 AUTO_INCREMENT=7 ;

关于php - 在数据库中插入 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12123895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com