aboji
aboji

Reputation: 37

insert an Unicode character in databsae

I am trying to insert an Unicode character like : ( πŸ˜„ ) in the database, but when I insert that ( πŸ˜„ ) in textarea, it adds an empty field?!

So how can I add characters like that πŸ˜„πŸ˜ŠπŸ˜ƒβ˜ΊπŸ˜‰πŸ˜πŸ˜˜πŸ˜šπŸ˜³πŸ˜ŒπŸ˜πŸ˜œπŸ˜ in database?

My database is:

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 to add data:

<!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 to insert and view data:

<!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>

Upvotes: 0

Views: 1564

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799580

πŸ˜„ U+1F604 SMILING FACE WITH OPEN MOUTH AND SMILING EYES

Note that it's beyond U+FFFF, which means it takes 4 bytes to store.

Make sure you're using MySQL 5.5 or higher and:

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

Upvotes: 1

Related Questions