Skip to content

Instantly share code, notes, and snippets.

@okjake
Created October 12, 2011 11:26
Show Gist options
  • Select an option

  • Save okjake/1280968 to your computer and use it in GitHub Desktop.

Select an option

Save okjake/1280968 to your computer and use it in GitHub Desktop.
Script to do a bulk change of Drupal 6 user email domains
<?php
/*
* Does a bulk change of email addresses stored in a Drupal 6 database, eg from *@example.com to *@new-example.com
*
* Ensure that a backup is taken before running this.
* 0. TAKE A BACKUP OF YOUR DATABASE - mysqldump -u[username] -p[password] [dbname] > [dbname].sql
* 1. Fill out the details below
* 2. Upload this somewhere accessible over the web
* 3. Point your browser to it
*/
$dbname = 'example_database';
$username = 'example_database_username';
$password = 'example_database_password';
$existing_domain = '@example.com';
$target_domain = '@new-example.com';
/* Thats it - stop editing */
try {
$dbh = new PDO("mysql:host=localhost;dbname=".$dbname, $username, $password);
$sql_select = 'SELECT uid,mail FROM users WHERE mail LIKE "%' . $existing_domain . '"';
$result = $dbh->query($sql_select);
foreach($result as $row) {
$user_id = $row['uid'];
$current_email = $row['mail'];
$updated_email = str_replace($existing_domain, $target_domain, $current_email);
$sql_update = 'UPDATE users SET mail=? WHERE uid=?';
$query = $dbh->prepare($sql_update);
$query->execute(array($updated_email, $user_id));
echo $current_email . ' => ' . $updated_email . '<br />';
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment