Jump to content

reducing queries - mysql insert & foreach


perky416

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.