Question

Actual String:

$sub"=?UTF-8?Q?=e2=99=a3?= Styles to =?UTF-8?Q?=e2=99=a3?="
UTF value is decoded using:
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('utf-8');
$subjectvalue=mb_decode_mimeheader($sub);

when I echoed $subjectvalue desired value displayed on my browser ,desired value is:

♣ Styles to  ♣

Issue: when I tried to insert value of $subjectvalue, value inserted looks like

♣ Styles to ♣

why value inserted is not as echoed on browser. How to manage and insert value of subject line ,if it is encoded.

Was it helpful?

Solution

When you connect to your MYSQL database try running just after connecting

SET names 'utf8'

I've prepared the whole test:

1) I've created database named testencoding

2) I've created sample table:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

CREATE TABLE IF NOT EXISTS `sample` (
`id` int(11) NOT NULL,
  `value` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;


ALTER TABLE `sample`
 ADD PRIMARY KEY (`id`);


ALTER TABLE `sample`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;

You can import it into database using phpmyadmin

3)

I've created test PHP script:

<?php
$sub = "=?UTF-8?Q?=e2=99=a3?= Styles to =?UTF-8?Q?=e2=99=a3?=";

header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('utf-8');
$subjectvalue=mb_decode_mimeheader($sub);
$subjectvalue2 = '会意字 / 會 huìyìzì ';
echo $subjectvalue."<br />";
echo $subjectvalue2."<br />";


$link = mysqli_connect("localhost","root","","testencoding");
mysqli_query($link,"SET NAMES 'utf8'");
mysqli_query($link,"INSERT INTO sample(`value`) VALUES('".mysqli_real_escape_string($link,$subjectvalue)."')");
mysqli_query($link,"INSERT INTO sample(`value`) VALUES('".mysqli_real_escape_string($link,$subjectvalue2)."')");


$result = mysqli_query($link, "SELECT * FROM sample");
echo "<br /><br />Data from database<br /><br />";
while ($data = mysqli_fetch_assoc($result)) {
    echo $data['id'].' '.$data['value']."<br />";
}

4) Results are:

In phpmyadmin:

Result from phpmyadmin

In PHP script:

Result from php

So to sum up, everything works as expected. Data are inserted as they should be, data in phpmyadmin and those read from database via php are the same as they were inserted

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top