Artur Stary
Artur Stary

Reputation: 744

PDO charset from iso-8859-2 to UTF8

I have a field in MySQL DB:

description     text    utf8_general_ci

I use CURL to scrap website with iso-8859-2 and try to insert values to utf8_general_ci field. Everyting is okay, but I lose characters ie. ęąśćż etc. Can you guys help me?

I connect to DB via PDO:

 $pdo = new PDO('mysql:host=localhost;dbname=xx;charset=utf8', 'xxx', 'xxx');
 $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

than I prepare my query:

 $stmt1 = $pdo -> prepare('INSERT INTO `jos_djcf_items1` (`cat_id`, `user_id`, `name`, `description`, `intro_desc`, `date_start`, `display`, `published`, `contact`)    VALUES(
                                :cat_id,
                                :user_id,
                                :name,
                                :description,
                                :intro_desc,
                                :date_start,
                                :display,
                                :published,
                                :contact)'); // 1

And I check charset:

$oferta[$j][0]= iconv("iso-8859-2","UTF-8",$nazwa);
$ary[] = "UTF-8";
$ary[] = "iso-8859-2";
$ary[] = "EUC-JP";
echo mb_detect_encoding($oferta[$j][0], $ary);
// it returns: UTF-8
$oferta[$j][1]=iconv("iso-8859-2","UTF-8",$krotkiOpis);
$oferta[$j][2]=iconv("iso-8859-2","UTF-8",$opis);
$oferta[$j][3]=iconv("iso-8859-2","UTF-8",$kontakt);
$oferta[$j][4]=rand(3,40);

than I bind Values and execute query

$count += $stmt1 -> execute(); 

Upvotes: 1

Views: 5157

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173522

Prior to PHP 5.3.6, this is how you should set the connection character set:

$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);

$pdo = new PDO('mysql:host=localhost;dbname=xx', 'xxx', 'xxx', $options);

Note that the encoding must be ASCII compatible.

Upvotes: 3

Related Questions