utf-8 convert

Hi,

first of all: If there is really no good reason to convert the charset, just don't do it!!

If you have an existing db which uses iso-8859-1 encoding and you want to switch to utf-8 you have to convert your db table content too.

Be aware: In some mysql versions there are bugs belonging to the utf-8 charset! (just search in the forums of ez.no and mysql.com!)

So, do the described steps above and write an "update.php" script.

Example code:



function &db_utf_query($selectsql ='', $tablesql='', $wheresql='', $limitsql='', $ordersql='')


{


   $db =& eZDB::instance();


   $db->setIsSQLOutputEnabled( false );


        $returnArray = $db->arrayQuery( "SELECT 

                $selectsql


        FROM


         $tablesql


         $wheresql


         $ordersql


         $limitsql


   " );


    

   return $returnArray;


}


 

$codepage = eZCodePage::instance("iso-8859-1", false);


 

$db =& eZDB::instance();


$db->setIsSQLOutputEnabled( false );


 

include_once ('lib/ezi18n/classes/ezutf8codec.php');


include_once ('lib/ezi18n/classes/ezcodepage.php');


 

$codepage = eZCodePage::instance("iso-8859-1", false);


 

$db =& eZDB::instance();


$db->setIsSQLOutputEnabled( false );


 

 

$selectsql      = "id, enumelement, enumvalue";


$tablesql       = "ezenumvalue";


$wheresql       = "";


$limitsql       = "";


$ordersql       = "";


 

 

$sqlarray = db_utf_query($selectsql, $tablesql, $wheresql, $limitsql, $ordersql);


 

$count_elements = count ($sqlarray);


echo "\n\n\n----------------------------------\n";


echo " Updating $tablesql - $count_elements items \n";


echo "----------------------------------\n";


 

 

if (count($sqlarray) > 0)


{


        foreach ($sqlarray as $item)


        {


                


                $update_item = $item;


                


                $update_item['enumelement'] = $codepage->convertString ($item['enumelement']);


                $update_item['enumvalue'] = $codepage->convertString ($item['enumvalue']);


                        


                $sql = "UPDATE 

                                `ezenumvalue` 

                        SET 

                                `enumelement` = '".$update_item['enumelement']."',


                                `enumvalue` = '".$update_item['enumvalue']."'


                        WHERE 

                                `id` = '".$update_item['id']."'";


                        


                $db->query($sql);


        }       


}


 

 

$selectsql      = "*";


$tablesql       = "ezcontentobject_attribute";


$wheresql       = " WHERE `data_text` NOT LIKE \"<?xml%\" AND data_text != \"\" ";


$limitsql       = "";


$ordersql       = "";


 

$sqlarray = db_utf_query($selectsql, $tablesql, $wheresql, $limitsql, $ordersql);


 

$count_elements = count ($sqlarray);


echo "\n\n\n----------------------------------\n";


echo " Updating $tablesql - $count_elements items \n";


echo "----------------------------------\n";


 

 

$i = 1;


if (count($sqlarray) > 0)


{


        foreach ($sqlarray as $item)


        {


                


                $update_item = $item;


                


                // echo "id: " . $item['id'] . " / Inhalt: " . $item['keyword'] . "\n";


                $update_item['data_text'] = $codepage->convertString ($item['data_text']);


                // echo $update_item['keyword'] . "\n";


                        


                $sql = "UPDATE 

                                `ezcontentobject_attribute` 

                        SET 

                                `data_text` = '".$update_item['data_text']."'


                        WHERE 

                                `id` = '".$update_item['id']."' 

                                AND `version` = '".$update_item['version']."' 

                                 ";


                        


                $db->query($sql);


                


                if ($i % 1000 == 0)


                        echo " $i. ";


                $i++;


        }       


}


 

$selectsql      = "*";


$tablesql       = "ezcontentobject_name";


$wheresql       = "";


$limitsql       = "";


$ordersql       = "";


 

$sqlarray = db_utf_query($selectsql, $tablesql, $wheresql, $limitsql, $ordersql);


 

$count_elements = count ($sqlarray);


echo "\n\n\n----------------------------------\n";


echo " Updating $tablesql - $count_elements items \n";


echo "----------------------------------\n";


 

 

$i = 1;


if (count($sqlarray) > 0)


{


        foreach ($sqlarray as $item)


        {


                


                $update_item = $item;


                


                // echo "id: " . $item['id'] . " / Inhalt: " . $item['keyword'] . "\n";


                $update_item['name'] = $codepage->convertString ($item['name']);


                // echo $update_item['keyword'] . "\n";


                        


                $sql = "UPDATE 

                                `ezcontentobject_name` 

                        SET 

                                `name` = '".$update_item['name']."'


                        WHERE 

                                `contentobject_id` = '".$update_item['contentobject_id']."' 

                                AND `content_version` = '".$update_item['content_version']."' 

                                 AND `content_translation` = '".$update_item['content_translation']."' 

                                 ";


                        


                $db->query($sql);


                


                if ($i % 500 == 0)


                        echo " $i. ";


                $i++;


        }       


}




(and so on)

If anyone needs such a convert script I will make a contribution, write to emil.webber@gmx.at.

Kind regards,
Georg.

Modified by Georg Franz on 29/10/2004 at 2:40:55 pm