1

I have a script that imports users.
Everytime a users is imported/added the ID increments.

The problem is that a lot of users (±75) are deleted at once and between 50 and 100 are added once a day.

Would it be possible to let the ID increment start at the last "active" ID so I won't have a database waste?

This is the script I use.(Whether it is good or not is not relevant for now)

$filepath = get_bloginfo('template_directory')."/import_test2.csv";
ini_set('auto_detect_line_endings',TRUE);
$file = fopen($filepath, "r") or die("Error opening file");
$i = 0;
while(($line = fgetcsv($file, 1000, ";")) !== FALSE) {
    if($i == 0) {
        $c = 0;
        foreach($line as $col) {
            $cols[$c] = $col;
            $c++;
        }
    } else if($i > 0) {
        $c = 0;
        foreach($line as $col) {
            $data[$i][$cols[$c]] = $col;
            $c++;
        }
    }
    $i++;
}
foreach ($data as $gebruiker){
    $username = $gebruiker['username'];
    if ( username_exists( $username ) && $gebruiker['status'] == 'published' ){
        $user = get_user_by( 'login', $username);
        update_user_meta( $user->ID, 'first_name', $gebruiker['first_name'] );
    }else{
        $users = wp_insert_user(
            array( 
                'user_login'    =>  $gebruiker['username'],
                'user_pass'     =>  $gebruiker['password'],
                'first_name'    =>  $gebruiker['first_name'],
                'last_name'     =>  $gebruiker['last_name'],
                'user_email'    =>  $gebruiker['email'],
                'display_name'  =>  $gebruiker['first_name'] . ' ' . $gebruiker['last_name'],
                'nickname'      =>  $gebruiker['first_name'] . '' . $gebruiker['last_name'],
                'role'          =>  'subscriber'
                )                       
            );
            foreach ($data as $update_user) {
                update_user_meta( $users, 'company', $gebruiker['bedrijf'] );
                update_user_meta( $users, 'function', $gebruiker['functie'] );
            }
        }if(username_exists( $username ) && $gebruiker['status'] == 'archived'){
        require_once(ABSPATH.'wp-admin/includes/user.php' );
        $user = get_user_by( 'login', $username);
        echo $user->ID.'<br>';
        wp_delete_user( $user->ID );
    }
}

I know the script adds users that don't exist and are deleted afterwards if the status is archived but that is something for later. Priorities are this now!

Hope there is a solution!

Interactive
  • 1,474
  • 5
  • 25
  • 57
  • 1
    Set mysql auto_increment counter to the maximum id, see http://stackoverflow.com/questions/8923114/how-to-reset-auto-increment-in-mysql Furthermore. I would not bother. – AmazingDreams Apr 09 '15 at 12:16
  • 2
    It doesn't really matter if you waste IDs for the most part. The default type for an ID is usually a 32-bit INT, meaning you can have around 2.1 billion numbers before you run out(and need to change to a LONG or 64-bit INT). This increases to about 4.5 billion if it is an unsigned 32-bit INT. As @AmazingDreams said, it's not really important. Besides, it might cause other issues if you're not handling foreign keys and such properly. – Frank Apr 09 '15 at 12:19
  • Ha nice. Thnx guys. If I can go up to around a couple of bilion I'll leave it at that! – Interactive Apr 09 '15 at 12:22
  • It does not sound like a very good idea to me to re-use an ID. While most likely irrelevant for your project, i would like to propose to steer clear of such habits. – Burki Apr 09 '15 at 12:36

0 Answers0