brown2005 Posted February 26, 2007 Share Posted February 26, 2007 Hi I have this code INSERT INTO `mytable` ( `id` , `number` , `number_two` , `field2` , `field1` ) VALUES (NULL , '50', '1', '', ''), (NULL , '50', '2', '', ''); what i want to do is replaced where the number 50 is with $i and insert the above from 1 - 49 can someone help please? thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/40191-insert-into-table-result-1-49/ Share on other sites More sharing options...
craygo Posted February 26, 2007 Share Posted February 26, 2007 ok so let me get this straight. so you want to start from 1 and go to 49. <?php for($i=1; $i<=49; $i++){ $sql = "INSERT INTO mytable ( `id` , `number` , `number_two` , `field2` , `field1` ) VALUES (NULL , '$i', '1', '', '')"; mysql_query($sql) or die (mysql_error()); } ?> This will loop 49 times and insert a record from 1 till it reaches 49. Not sure if I have the value in the right place but just put the $i in the place where you want the increment number to go Ray Quote Link to comment https://forums.phpfreaks.com/topic/40191-insert-into-table-result-1-49/#findComment-194454 Share on other sites More sharing options...
brown2005 Posted February 26, 2007 Author Share Posted February 26, 2007 exactly what I needed. Thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/40191-insert-into-table-result-1-49/#findComment-194483 Share on other sites More sharing options...
ToonMariner Posted February 26, 2007 Share Posted February 26, 2007 This is more efficient... (you had it from the off btw) <?php <?php $sql = "INSERT INTO mytable ( `id` , `number` , `number_two` , `field2` , `field1` ) VALUES"; for($i=1; $i<=49; $i++){ $sql .= " (NULL , '$i', '1', '', ''),"; } $sql = substr($sql,0,-1); mysql_query($sql) or die (mysql_error()); ?> Only run one query there which should be A LOT faster. Quote Link to comment https://forums.phpfreaks.com/topic/40191-insert-into-table-result-1-49/#findComment-194489 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.