A script to change all tables and fields to the utf-8-bin collation in MYSQL

后端 未结 16 1034
有刺的猬
有刺的猬 2020-12-04 06:24

Is there a SQL or PHP script that I can run that will change the default collation in all tables and fields in a database?

I can write one

16条回答
  •  天涯浪人
    2020-12-04 07:11

    I think the fastest way is with phpmyadmin and some jQuery on console.

    Go to table's structure and open chrome/firefox developer console (normally F12 on keyboard):

    1. run this code to select all fields with incorrect charset and start modify:

      var elems = $('dfn'); var lastID = elems.length - 1;
      elems.each(function(i) {
          if ($(this).html() != 'utf8_general_ci') { 
             $('input:checkbox', $('td', $(this).parent().parent()).first()).attr('checked','checked');
          }       
      
          if (i == lastID) {
              $("button[name='submit_mult'][value='change']").click();
          }
      });
      
    2. when page is loaded use this code on console to select correct encoding:

      $("select[name*='field_collation']" ).val('utf8_general_ci');
      
    3. save

    4. change the table's charset on "Collation" field on "Operation" tab

    Tested on phpmyadmin 4.0 and 4.4, but I think work on all 4.x versions

提交回复
热议问题