I have a temp table (a dump from older system, with one long row of many details) with about 8k records, I need to break it down, use some logic and insert the data into 3 different tables.
table_temp (id, name, address, email, phone, mobile, etc......)
tbluser (id, username, password, roleid, status)
tblstudent (id, userid, name, address, classid, sectionid, etc.....)
tblstudentdetails(id, studentid, address, contact details....)
Since I need to get the data, put some logic and create separate insert statements to all these tables. I have tried to create large insert statements like:
INSERT INTO `tbluser`(`instsessassocid`, `username`, `password`, `roleid`, `status`) VALUES
('1','PRAGUN10000@temp.com','$2y10$gFscDWwWKR8Iven6R','3','1'),
('1','SONAM10001@temp.com','$2y10$gFscDWwWKR8Iven6R','3','1'),
('1','SONAM10001@temp.com','$2y10$gFscDWwWKR8Iven6R','3','1')
And the same for other tables (you get the idea):
INSERT INTO `tblstudent`(`scholarnumber`,`firstname`, `middlename`,`lastname`) VALUES
('10000','PRAGUN','','TANWAR'),('10001','SONAM','','-'),
('10002','HITESH','','KUMAR'),('10003','KHUSHI','','MEHTA'),
('10004','ADITYA','','VASYANI')
After creating 4 large insert statements, I store them in an array and send it to my insert function, which put off auto-commit, takes each insert statement and once all is ok, commits and should return the insert ids etc.
My app seems to time-out (currently set to default/30 sec), surely such a small insert shouldn't time out, can anyone point out what I am doing wrong or how I can improve.
This is my insert function:
function dbInsert($sql) {
if (is_array($sql) == 0) {
$sqlarray[] = $sql;
} else {
$sqlarray = $sql;
}
$sqlCount = count($sqlarray); //echoThis($sqlCount); die;
$con = dbConnect();
$insertID = array();
try {
// begin a transaction
$con->autocommit(FALSE);
/* commit transaction */
foreach ($sqlarray as $value) {
if ($con->query($value)) {
$insertID[] = $con->insert_id;
} else {
trigger_error(mysqli_error($con));
}
}
// if no error, commit.
if ((!mysqli_error($con)) || (!mysqli_commit($con)) && ($sqlCount === count($insertID))) {
$con->commit(); //mysqli_commit($con);
} else {
$con->rollback();
trigger_error("Error in dbInsert: " . mysqli_error_list($con));
$con->close();
}
} catch (Exception $e) {
// if any error, catch the exception and rollback
$con->rollback();
trigger_error("Error in dbInsert:" . $e);
}
/* close connection and return the result */
$con->close();
return $insertID;
}
This is just a test, once sorted I would need to import quite a few records so I want to have "ready scripts" which I can use as needed.
I am aware of load infile or mysqlimport ways but I need the logic and I really think my scripts should be able to do the job. Mind you I am on my localhost, with nothing else happening with a reasonable configuration machine.
Thank you!