7

I'm following this to make my plugin auto-create a table when the plugin is activated, but what happens is that while all the tables (the whole db) are utf8_general_ci, the newly created table is latin1_swedish_ci... Why isn't it also utf8? I thought it would also be utf8 by default since I have:

define('DB_COLLATE', 'utf8_general_ci');

in my wp-config.php... I have everything exactly the same like in the link provided, except the function name, and I have different SQL fields... How to make the newly created table utf8 by default?

this is my function:

function duende_install() {
global $wpdb;
global $duende_db_version;

$table_name = $wpdb->prefix . "duendes";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
    $sql = "CREATE TABLE " . $table_name . " (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        shape tinytext NOT NULL,
        integrity tinytext NOT NULL,
        length tinytext NOT NULL,
        drone tinytext NOT NULL,
        wood tinytext NOT NULL,
        mouth tinytext NOT NULL,            
        rim tinytext NOT NULL,
        bell tinytext NOT NULL,                     
        loud tinytext NOT NULL,                     
        mass tinytext NOT NULL,                     
        finish tinytext NOT NULL,   
        inlay tinytext NOT NULL,                    
        price smallint DEFAULT '0' NOT NULL,
        sold tinytext NOT NULL,                                             
        UNIQUE KEY id (id)
    );";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);

    add_option("duende_db_version", $duende_db_version);
}
}

Thank you

4
  • Please add your CREATE TABLE SQL statement to your question, probably with the related PHP code.
    – hakre
    Commented Dec 25, 2010 at 20:39
  • here, I put the whole function in my question
    – tkit
    Commented Dec 25, 2010 at 20:47
  • 3
    Okay, it shows that you do not specify the COLLATION while creating the table. It will inherit the collation from the database which is latin1_swedish_ci. Wordpress sets the collation only for the connection, not for the database. E.g. for WHERE or ORDER BY clauses.
    – hakre
    Commented Dec 26, 2010 at 2:32
  • Please see this answer if you need an easy way to convert the collation on an existing WordPress plugin database: stackoverflow.com/questions/105572/…
    – dtbaker
    Commented Jun 17, 2014 at 2:34

4 Answers 4

5

You're missing the point that there is collation not only for the DB but as well for tables and even fields.

Therefore from your point of view, I assume that your CREATE TABLE statement is "not complete". Before using SQL statements, please learn the language first.

See CREATE TABLE Syntax (MySQL Manual).

Especially table_options / table_option and [DEFAULT] COLLATE [=] collation_name there in.

UPDATE:

Please see Wordpress Database Charset and Collation Configuration for an in-depth description where and how to setup wordpress regarding charset and collation.

3
  • well, I read somewhere in MySQL documentation that tables should by default inherit collation from the database... and here in my case - it didn't happen. that's why I asked... I can't find it again.. so, it's not inheritable and I should use COLLATE option in my CREATE TABLE statement?
    – tkit
    Commented Dec 25, 2010 at 21:15
  • 1
    @pootzko: I don't think that you have a special database that is not acting like the manual says. Your database's collation is latin1_swedish_ci. You can verify this anytime with the mysql commandline tool or phpmyadmin. So inheration does work. See dev.mysql.com/doc/refman/5.0/en/charset-table.html and dev.mysql.com/doc/refman/5.0/en/charset.html . Only by throwing some commands on each connect does not change the collation of the DB, it changes it only for the connection. See dev.mysql.com/doc/refman/5.0/en/charset-connection.html :SET NAMES + COLLATION, used by WPDB
    – hakre
    Commented Dec 26, 2010 at 2:30
  • 1
    sorry, I now see that the default DB collation is indeed latin1_sweedish_ci.. since I saw all wp tables as utf8 in phpmyadmin I thought DBs collation would also be utf8 and I really thought I set the whole DB to be utf8... but instead it was WP who takes care of those basic tables for us if we don't set the DBs collation right.. I just wasn't careful enough, sorry. and thank you hakre
    – tkit
    Commented Dec 26, 2010 at 9:40
8

just in case somebody does need this, you could set the collation this way.. the row after the UNIQUE KEY id (id) part should look like this:

) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
3
  • 1
    I now blogged about it: hakre.wordpress.com/2010/12/26/… --- this has some info how to keep this clean from the beginning of the installation as well.
    – hakre
    Commented Dec 26, 2010 at 13:44
  • 2
    If you do this in a plugin, it might be better to use th $wpdb->charset and $wpdb->collate settings, so other users of your plugin can override this if they have a good reason. See the first lines for wp-admin/includes/schema.php.
    – Jan Fabry
    Commented Dec 27, 2010 at 8:23
  • 2
    Thanks for actually answering this question with a real usable quick answer unlike the selected answer which rudely tells you to go read a ####ing manual
    – JasonDavis
    Commented May 25, 2013 at 0:49
1

The $wpdb object now has a member function for retrieving the character set and collation as a string that can be added directly to SQL CREATE TABLE queries.

$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name ( $fields ) $charset_collate;"

This string will line up with the WP_CHARSET and WP_COLLATE values from the configuration file (if present), and is used internally to create Wordpress's default tables.

0

I've been using this

  global $wpdb;
  // $charset_collate = $wpdb->get_charset_collate(); //This outputs an unsupported charset for a lot of non english speakers for some reason
  $charset_collate = "";
  $collation = $wpdb->get_row("SHOW FULL COLUMNS FROM {$wpdb->posts} WHERE field = 'post_content'");

  if(isset($collation->Collation)) {
    $charset = explode('_', $collation->Collation);

    if(is_array($charset) && count($charset) > 1) {
      $charset = $charset[0]; //Get the charset from the collation
      $charset_collate = "DEFAULT CHARACTER SET {$charset} COLLATE {$collation->Collation}";
    }
  }

  if(empty($charset_collate)) { $charset_collate = $wpdb->get_charset_collate(); } //Okay fine, we'll try it your way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.