Programmatically creating users attach profiles to correct uid on localhost but uid 1 on production While programmatically importing thousands of users from an old site, I'm facing this very strange behaviour. The code snippet below correctly attach commerce customer profiles to the created users on localhost site, but attach them to uid 1 on production site ! 

localhost is a strict copy of remote site : git clone for code and Backup and Migrate DB copy The users are imported uploading and parsing a csv file in a custom form, connected as admin    

$new_user = array(
  'name' =&gt; $new_name,
  'pass' =&gt; $pwd,
  'mail' =&gt; $email,
  'init' =&gt; $email,
  'status' =&gt; 1,
);

$account = new stdClass;
$account-&gt;roles = array(
  DRUPAL_AUTHENTICATED_RID =&gt; 'authenticated user',
  9 =&gt; 'client',
);

if ($saved_user = user_save($account, $new_user)) {
  dpm($saved_user, $new_name.'/'.$saved_user-&gt;uid );
  //VERY STRANGE HERE (ON PROD SITE ONLY) see screen capture below
  //the dpm title prints $saved_user-&gt;uid as a new uid
  //but "inside" dpm $saved_user-&gt;uid is 1 ! 
}

//create user commerce profile
$profile = commerce_customer_profile_new('billing', $saved_user-&gt;uid);
$profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
//... add profile values ...
$profile_wrapper-&gt;save();    


Localhost : expected uid when dpm($saved_user, $new_name.'/'.$saved_user-&gt;uid );


Production site : uid 1 instead. The user is created with the new uid though, but uid 1 is passed then to commerce customer profile
Sounds like somewhere in the first few lines of code your $account variable is getting switched to the user 1 or perhaps the currently logged in user. Perhaps trying to change the "$account" variable name to something definitely unique to see if that fixes the problem.