2012-08-26 31 views
-2

可能重复(?):
iPhone emoticons insert into MySQL but become blank value
insert an Unicode character in databsae插入问号到数据库

我试图插入Unicode字符,如:数据库(),但是当我插入()在textarea它添加一个问号(?)在数据库中?

DB

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> 
+0

为什么你再次发布相同的问题? http://stackoverflow.com/questions/12123895/insert-an-unicode-character-in-databsae – snuffn

+0

问题现在在问号 – aboji

回答

0

SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci

的另一件事是(如documentation says)已过时mysql_不支持的字符集。

+0

但说实话,我不知道是'mysql_'支持'uttf8mb4'。 – Peter

+0

如果它v5.5它支持它...但它不起作用 – aboji

+0

我的意思是不推荐'mysql_'函数。你应该使用PDO或者提供'mysqli_'。请阅读:http://www.php.net/manual/en/mysqli.overview.php – Peter