Jump to content

Recommended Posts

Hi everyone.

 

Iv touched on something similar to this before however having come back to it after a while i dont fully understand it.

I have a website where people can add their domains. They add multiple domains in a text area. I have code that splits up each line of the text box and validates each domain etc...that is all working fine. Below is the code that I currently use to insert each domain into the database.

If the user adds 100 domains, i take it that the code below will execute 100 queries to write each domain?

 

Is it possible to execute only 1 query and add all the domains, each to a new row in the database?

 

$not_exists = array_diff(array_map('trim', $lines), $exists);
foreach ($not_exists as $domain){
mysql_query("INSERT INTO domains VALUES("'$domain'");
}

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/241598-reducing-queries-mysql-insert-foreach/
Share on other sites

If saved the urls information as text or cvs you can cycle the lists with LOAD DATA INFILE

http://dev.mysql.com/doc/refman/5.6/en/load-data.html

 

LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name
  FIELDS TERMINATED BY ',' ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'
  IGNORE 1 LINES;

Hi mate,

Thanks for the tip however that wasn't really what I was looking for. Im hoping to do it just by altering the code if its possible. I could have loads of users adding loads of domains all at the same time. I don't really want to be creating files on the server.

Thanks anyway.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.