【问题标题】:insert an Unicode character in databsae在数据库中插入一个 Unicode 字符
【发布时间】:2012-08-25 16:53:28
【问题描述】:

我正在尝试在数据库中插入一个 Unicode 字符,例如: ( ???? ),但是当我在 textarea 中插入该 ( ???? ) 时,它会添加一个空字段?!

那么我怎样才能添加这样的字符????????????☺??????????????????????????? ??????在数据库中?

我的数据库是:

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>

【问题讨论】:

    标签: php mysql database unicode insert


    【解决方案1】:

    ? U+1F604 笑脸张开嘴笑眼

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

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

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

    【讨论】:

    • 我有这个错误.. #1115 - 未知字符集:'utf8_mb4'
    猜你喜欢
    • 2011-08-02
    • 2011-03-23
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    相关资源
    最近更新 更多