PDA

View Full Version : Migrating the "profile_" fields from Drupal's users table


AtlasApollo
02-05-2009, 11:44 PM
We are using vbDrupal 5.1Beta and I needed to migrate the profile data for the users.

Here is the script I wrote to do so, you are welcome to use it. Please be sure to modify the field order in the update statement at the bottom of the script, because your vBulletin profile fields might not line up with ours.

Hope it helps someone.


<?php
mysql_connect("PutYourHostHere", "UserName", "Password*") or
die("Could not connect: " . mysql_error());
mysql_select_db("YourDBHere");

// Get all of the users and their data record
$result = mysql_query("SELECT uid, data FROM drupal_users");

// This boolean will flipFlop, and will tell the code whether it is looking for the key or the value
$processingKey = True;
$processingValue = False;

// loop through all of the data records. Get the profile data;
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
$uid = $row[0];
$unser = unserialize($row[1]);

$LastName = "";
$FirstName = "";
$Company = "";
$Title = "";
$Address1 = "";
$Address2 = "";
$City = "";
$State = "";
$Country = "";
$Zip = "";
$ShirtSize = "";

$keyToProcess = "";

foreach ($row as $field => $value)
{
$rowArr = explode(";", $value);

foreach($rowArr as $exploded)
{
if (strpos($exploded, "profile_") <> 0)
{
$processingKey = True;
}

if ($processingKey)
{
// get value between the quotes
$realVal = substr($exploded, strpos($exploded, "profile_") + strlen("profile_"), -1);

if (strlen($realVal) > 0)
{
$keyToProcess = $realVal;
$processingValue = True;
$processingKey = False;
}
else
{
$keyToProcess = "";
$processingValue = False;
$processingKey = False;
}

$processingKey = False;
}
else if ($processingValue == True)
{
$explodedValArr = explode('"', $exploded);
$daValue = $explodedValArr[1];

if ($keyToProcess == "firstname")
{
$FirstName = $daValue;
}

if ($keyToProcess == "lastname")
{
$LastName = $daValue;
}

if ($keyToProcess == "company")
{
$Company = $daValue;
}

if ($keyToProcess == "title")
{
$Title = $daValue;
}

if ($keyToProcess == "address1")
{
$Address1 = $daValue;
}

if ($keyToProcess == "address2")
{
$Address2 = $daValue;
}

if ($keyToProcess == "city")
{
$City = $daValue;
}

if ($keyToProcess == "zip")
{
$Zip = $daValue;
}

if ($keyToProcess == "state")
{
$State = $daValue;
}

if ($keyToProcess == "country")
{
$Country = $daValue;
}

if ($keyToProcess == "shirtsize")
{
$ShirtSize = $daValue;
}

$processingValue = False;
$processingKey = False;
$keyToProcess = "";
}
}
}


$updateSql = "update userfield set field4 = '$Title', field5 = '$FirstName', field6 = '$LastName', field7 = '$Address1', field8 =
'$Address2', field9 = '$Company', field10 = '$City', field11 = '$State', field12 = '$Country', field13 = '$ShirtSize', field14 = '$Zip' where
userid = $uid";

mysql_query($updateSql);

echo "uid $uid updated.<BR>";
}

mysql_free_result($result);
?>


Cheers,
Mike