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 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 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 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. 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
Archived
This topic is now archived and is closed to further replies.