perky416 Posted July 10, 2011 Share Posted July 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241598-reducing-queries-mysql-insert-foreach/ Share on other sites More sharing options...
QuickOldCar Posted July 10, 2011 Share Posted July 10, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/241598-reducing-queries-mysql-insert-foreach/#findComment-1240946 Share on other sites More sharing options...
perky416 Posted July 10, 2011 Author Share Posted July 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241598-reducing-queries-mysql-insert-foreach/#findComment-1240947 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.