MyBB Community Forums

Full Version: sql query for change username?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there any sql query to change bullk usernames?
I want to change some usernames from username_mybb_import to username
Doing this kind of thing in a query is extremely process intensive. It is much better to select usernames, use a regex on them, then perform an update query rather than doing it all in just SQL. I'd also do it over several pages rather than all at once to avoid timeout issues.

Name this file changenames.php.
<?php
define("IN_MYBB", 1");
require_once "global.php";
if(!$mybb->usergroup['cancp'])
{
error_no_permission();
}
$query = $db->query("SELECT uid, username FROM " . TABLE_PREFIX . "users WHERE username LIKE '%_mybb_import' LIMIT 50");
if($db->num_rows($query) == 0)
{
die("Username changes complete.");
}
while($result = $db->fetch_array($query))
{
$newusername = preg_replace("/\A(.*)(mybb_import){1}\Z/is", "$1", $result['username']);
$uid = $result['uid'];
echo "<br />Changed username " . $result['username'] . " to " . $newusername;
$db->write_query("UPDATE " . TABLE_PREFIX . "users SET username='$newusername' WHERE uid=$uid");
}
redirect("changenames.php", "Redirecting to next page.");
?>