I have two mysql tables and I want to insert a letter into them.
I need to split the email into nameand domain nameand paste each part of them into one table.
My table domainis as follows:
+----+--------+
| id | domain | primary(id)
+----+--------+
and my table email:
+-------+----------+
| eMail | domainId | primary key(eMail, domainId)
+-------+----------+
ie: info@example.com > example.commust be inserted into the table domainand
infomust be added to the table emailwith domainId.
The domain must be unique.
My attempt:
foreach($emails as $email)
{
list($account,$hostname) = explode('@',$email,2);
$query = $dbh->prepare("SELECT id FROM domain WHERE domain LIKE :domain LIMIT 0,1");
$query->execute(array(':domain'=>trim($hostname)));
if($query->rowCount())
{
$id = $query->fetch();
$id = $id['id'];
}else{
$insert = $dbh->prepare("INSERT INTO domain (domain) VALUES (:domain)");
$insert->execute(array(':domain'=>trim($hostname)));
$id = $dbh->lastInsertId();
}
$name = $dbh->prepare("INSERT INTO email (eMail, domainId) VALUES (:eMail, :domainId)");
$name->execute(array(':eMail'=>trim($account),':domainId'=>$id));
}
but this method is very slow when I want to add bulk emails:
Is there any faster way or maybe I can do this in one request ..?