OK, I've taken the bull byt the horns, and tried to do it myself. I've edited Christians code to suit my own problem. Does this look like it will work as required above?
//script trouvé sur phpfrance
//http://www.phpfrance.com/tutorials/index.php?page=3&id=32
// variables
$bdd= "import_test";
$host= "localhost";
$user= "root";
$pass= "";
$fichier = "users2.csv";
/* Description for data file
uname;name;email
Delete the last CRLF on last line
*/
mysql_connect($host,$user,$pass) or die("Unable to connect Database");
mysql_select_db($bdd);
// Open file for read
if (file_exists($fichier))
$fp = fopen("$fichier", "r");
else{ // unknown file
echo "File not found !
Import stopped.";
exit();
}
echo 'Begin read file import '.$fichier.'
';
// import line by line. Re-order as per spreadhseet.
while (!feof($fp)){
$ligne = fgets($fp,4096);
$liste = explode(";",$ligne); // create array
$user = $liste[0]; // 1st field - username
$pass= sprintf("%sn",md5($liste[1])); // 2nd field - password
$name= $liste[2]; // 3rd field - Real Name, use 'point of contact' value
$email= $liste[3]; // 4th field - E-Mail
$location= $liste[4]; // 5th field - Use for the company name
// Add line in xoops_users table
$query = "INSERT INTO xoops_users (uname, pass, name, email, user_from) VALUES('$user','$pass','$name','$e-mail','$location',)";
$result= mysql_query($query);
$uid = mysql_insert_id();
if (mysql_error()){
echo "Error in database : ".mysql_error();
echo "
Import stopped.";
fclose($fp);
exit();
}
//Add user in group
$numgroup ='2';
$query = "INSERT INTO xoops_groups_users_link (groupid, uid) VALUES('$numgroup', '$uid')";
$result= mysql_query($query);
//echo 'item add = '.$uid;
if (mysql_error()){
echo "Error in database : ".mysql_error();
echo "
Import stopped.";
fclose($fp);
exit();
}else{
echo "item ".$uid." - ".$user." add
";
}
}
echo "
Import successfully";
fclose($fp);
You'll see that what I've done is remove the e-mail check and creation, because in my case there is always an e-mail address anyway, so it just needs to read it in.
I've assumed that each slot in the array comprises 4096 bytes of memory, starting from slot 0.
Rather than create a password based on the username I've changed it to read in the value from the text file and then do the MD5 on it. That way the password will not be changed, yes?
Basically, in theory, if I have understood correctly, it should read in the input file column by column until it reaches the end, and then just insert (into the database) the field values (in my case, 5 columns)? Yes? Or have I got the wrong end of the stick. First attempt at PHP.
Thanks
Ted