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
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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.