Fix serialized data broken due to editing MySQL database in a text editor?

后端 未结 6 615
青春惊慌失措
青春惊慌失措 2020-12-30 02:25

Background: I downloaded a *.sql backup of my WordPress site\'s database, and replaced all instances of the old database table prefix with a new on

6条回答
  •  悲&欢浪女
    2020-12-30 03:21

    I personally don't like working in PHP, or placing my DB credentials in an public file. I created a ruby script to fix serializations that you can run locally:

    https://github.com/wsizoo/wordpress-fix-serialization

    Context Edit: I approached fixing serialization by first identifying serialization via regex, and then recalculating the byte size of the contained data string.

    $content_to_fix.gsub!(/s:([0-9]+):\"((.|\n)*?)\";/) {"s:#{$2.bytesize}:\"#{$2}\";"}
    

    I then update the specified data via an escaped sql update query.

    escaped_fix_content = client.escape($fixed_content)
    
    query = client.query("UPDATE #{$table} SET #{$column} = '#{escaped_fix_content}' WHERE #{$column_identifier} LIKE '#{$column_identifier_value}'")
    

提交回复
热议问题