Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save lavoiesl/d7a41597c8023978cab7 to your computer and use it in GitHub Desktop.

Select an option

Save lavoiesl/d7a41597c8023978cab7 to your computer and use it in GitHub Desktop.
<?php
// $pdo = new PDO(...)
$charsets = array('UTF-8', 'ISO-8859-1');
$tables = array(
'events' => array(
'title',
'description',
),
'locations' => array(
'title',
'description',
),
);
foreach ($tables as $table => $fields) {
$result = $pdo->query('SELECT id, ' . implode(', ', $fields) . ' FROM ' . $table);
while ($row = $result->fetch()) {
foreach ($fields as $field) {
if (($encoding = mb_detect_encoding($row[$field], $charsets, true)) != 'UTF-8') {
echo $table . '[' . $row['id'] . ']' . '.' . $field . ': ' . $encoding . "\n";
$converted = iconv($encoding, 'UTF-8', $row[$field]);
$stmt = $pdo->prepare('UPDATE ' . $table . ' SET ' . $field . ' = ? WHERE id = ?');
$stmt->execute(array($converted, $row['id']));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment