【问题标题】:Insert question mark(?) into database [duplicate]将问号(?)插入数据库[重复]
【发布时间】:2012-08-26 10:18:44
【问题描述】:

可能重复:
iPhone emoticons insert into MySQL but become blank value
insert an Unicode character in databsae

我正在尝试在数据库中插入一个 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_mb4 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】:

SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci

另一件事是(如documentation says)已弃用mysql_ 不支持字符集。

【讨论】:

  • 但老实说我不确定mysql_是否支持uttf8mb4。
  • 如果 v5.5 支持它......但它没有工作
  • 我的意思是已弃用的mysql_ 函数。您应该使用 PDO 或首选 mysqli_。请阅读:php.net/manual/en/mysqli.overview.php
  • 另外,整理只是关于排序优先级
  • @aboji 这只是解决方法,也是不好的做法。您应该将文本数据保存在文本字段中
猜你喜欢
  • 2016-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 2013-07-10
  • 1970-01-01
相关资源
最近更新 更多