add a user and role programatically I am using the following code to add a user:

  $roles = user_roles();
  $userinfo = array(
  'name' =&gt; 'user2',
  'pass' =&gt; '123456',
  'init' =&gt; 'user2d',
  'status' =&gt; 1,
  'access' =&gt; REQUEST_TIME,
  // This is where we are different from the default function.
  'roles'        =&gt; array(array_search('student', $roles) =&gt; 6),

);
$account = user_save(drupal_anonymous_user(), $userinfo);


I am getting this error message:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 19 column name is not        unique: 
INSERT INTO {users} (uid, name, pass, created, access, status, init) VALUES (?, ?, ?, ?, ?, ?, ?); Array ( [0] =&gt; 15 [1] =&gt; user2 [2] =&gt; $S$DrkIg0KuBFO12eQ9nQ5ViSx4ydfcWS0nS1TMvaTjYyZyfmVniGgh [3] =&gt; 1333135061 [4] =&gt; 1333135061 [5] =&gt; 1 [6] =&gt; user2d ) in drupal_write_record() 
(line 6975 of 


I am wondering if someone can explain what is going wrong with me trying to add a role. ......
drupal_anonymous_user() gives you an object with a uid that is considered empty by PHP. This means, your call to user_save() will try to insert a new user into the database. The insert operation fails, because a user with the name user2 already exists in the database.