We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    I’ve got my script to convert databases to utf8 (or any other character set) working, more or less, but one thing is still driving me crazy.

    The script generates many lines of SQL queries to paste into PhpMyAdmin. If I do an Export of the DB before and after, the before tables look like this:

    --
    -- Table structure for table `modx_active_users`
    --

    CREATE TABLE IF NOT EXISTS `modx_active_users` (
      `internalKey` int(9) NOT NULL default '0',
      `username` varchar(50) collate latin1_general_ci NOT NULL default '',
      `lasthit` int(20) NOT NULL default '0',
      `id` int(10) default NULL,
      `action` varchar(10) collate latin1_general_ci NOT NULL default '',
      `ip` varchar(20) collate latin1_general_ci NOT NULL default '',
      PRIMARY KEY  (`internalKey`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Contains data about active users.';


    After the conversion, the same table looks like this in the export:

    --
    -- Table structure for table `modx_active_users`
    --
    CREATE TABLE IF NOT EXISTS `modx_active_users` (
      `internalKey` int(9) NOT NULL default '0',
      `username` varchar(50) NOT NULL default '',
      `lasthit` int(20) NOT NULL default '0',
      `id` int(10) default NULL,
      `action` varchar(10) NOT NULL default '',
      `ip` varchar(20) NOT NULL default '',
      PRIMARY KEY  (`internalKey`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Contains data about active users.';


    The charset and collation are missing for individual text columns and the table collation is also missing.

    Here’s the relevant SQL:

    ALTER TABLE modx_active_users MODIFY username BLOB;
    ALTER TABLE modx_active_users MODIFY action BLOB;
    ALTER TABLE modx_active_users MODIFY ip BLOB;
    ALTER DATABASE dummy CHARSET utf8 COLLATE utf8_general_ci;
    ALTER TABLE modx_active_users MODIFY username varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci;
    ALTER TABLE modx_active_users MODIFY action varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci;
    ALTER TABLE modx_active_users MODIFY ip varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci;
    ALTER TABLE modx_active_users DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;


    All the character sets and collations are set correctly when I look in the database (after conversion) and the data in each table’s text fields appears to be converted to utf8.

    I can’t figure out why the explicit charset and collation settings aren’t there in the export after conversion. I’ve tried everything I can think of to put them there, but nothing has worked.
      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 22303 MODX Staff
      • 10,725 Posts
      You have to use the --create-options switch on mysqldump to get all the create table output (see http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_create-options), but my phpmyadmin definitely is exporting with all the field-specific collations by default in my tests. huh
        • 3749
        • 24,544 Posts
        Quote from: OpenGeek at Apr 17, 2010, 02:15 PM

        You have to use the --create-options switch on mysqldump to get all the create table output (see http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_create-options), but my phpmyadmin definitely is exporting with all the field-specific collations by default in my tests. huh

        It’s a puzzler. Both files were exported in PhpMyAdmin with the same options. That implies that the column settings aren’t being set, but the Queries should set them (and appear to). I’m baffled.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 3749
          • 24,544 Posts
          I think I see what’s happening. Both of the dump files have this at the top:

          /*!40101 SET NAMES utf8 */;

          Apparently, the individual charset and collation directives only get printed if they differ from what is set in the SET NAMES directive (or its default collation). If I change the collation to utf8_unicode_ci, I get this:

          --
          -- Table structure for table `modx_active_users`
          --
          
          CREATE TABLE IF NOT EXISTS `modx_active_users` (
            `internalKey` int(9) NOT NULL default '0',
            `username` varchar(50) collate utf8_unicode_ci default NULL,
            `lasthit` int(20) NOT NULL default '0',
            `id` int(10) default NULL,
            `action` varchar(10) collate utf8_unicode_ci NOT NULL default '',
            `ip` varchar(20) collate utf8_unicode_ci NOT NULL default '',
            PRIMARY KEY  (`internalKey`)
          ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Contains data about active users.';
          
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting