denoteone Posted November 9, 2009 Share Posted November 9, 2009 Is it ok to run a for loop to insert data into a database? I have $sql="INSERT INTO prices (box0,box1,box2,box3,box4,box5,box6,box7,box8,box9,box10,box11,box12,box13,box14,box15,box16,box17,box18,box19,box20,box21,box22,box23,box24,box25,box26,box27,box28,box29,box30,box31,box32,box33,box34,box35,box36,box37,box38,box39,box40,box41,box42,box43) VALUES ('$box_0','$box_1','$box_2','$box_3','$box_4','$box_5','$box_6','$box_7','$box_8','$box_9','$box_10','$box_11','$box_12','$box_13','$box_14','$box_15','$box_16','$box_17','$box_18','$box_19','$box_20','$box_21','$box_22','$box_23','$box_24','$box_25','$box_26','$box_27','$box_28','$box_29','$box_30','$box_31','$box_32','$box_33','$box_34','$box_35','$box_36','$box_37','$box_38','$box_39','$box_40','$box_41','$box_42','$box_43')"; mysql_query($sql) or die(mysql_error()); $query = "FLUSH PRIVILEGES"; mysql_query($query) or die(mysql_error()); it would be less of a mess if i could loop through it but I am not sure if this is good practice or not. Any thoughts would be awesome. Quote Link to comment https://forums.phpfreaks.com/topic/180886-loop-through-sql-query/ Share on other sites More sharing options...
ram4nd Posted November 9, 2009 Share Posted November 9, 2009 For loop would be better for doing this indeed. But I would make the string with for loop and upload it only once, i think like that it would be much faster. Quote Link to comment https://forums.phpfreaks.com/topic/180886-loop-through-sql-query/#findComment-954340 Share on other sites More sharing options...
Mchl Posted November 9, 2009 Share Posted November 9, 2009 How exactly running 43 queries is better than running only a one? And are you sure you know what are you doing? You're inserting a single row with 43 columns. Not 43 separate rows. Quote Link to comment https://forums.phpfreaks.com/topic/180886-loop-through-sql-query/#findComment-954368 Share on other sites More sharing options...
denoteone Posted November 12, 2009 Author Share Posted November 12, 2009 For this function I have a menu board GUI that has 43 areas to add pricing. each row has a column for that pricing area. Does this make sense? Each row would be an update made to the pricing fields. If there is a better way of doing this please let me know. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/180886-loop-through-sql-query/#findComment-956271 Share on other sites More sharing options...
Mchl Posted November 12, 2009 Share Posted November 12, 2009 http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html Quote Link to comment https://forums.phpfreaks.com/topic/180886-loop-through-sql-query/#findComment-956284 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.