Jump to content

insert into table result 1 - 49


brown2005

Recommended Posts

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

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

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.

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.