Dear all,

I have a table in mysql and i want to insert records into the table without using "FOR" or "WHILE" loop..Can this be done??

Thanks in advance..

Jino.

Recommended Answers

What do you mean by without using for or while loop ? What's the problem in inserting a record directly ? ie., insert into tablename (col1,col2) values ('val1','val2')

Jump to Post

If you know the values, you can do something like this.

insert into table (col1) values (val1),(val2),(val3)

This will insert 3 records to the table.

Jump to Post

All 8 Replies

What do you mean by without using for or while loop ? What's the problem in inserting a record directly ? ie., insert into tablename (col1,col2) values ('val1','val2')

not that i want to insert More than 100 records into my database with a single query.... I need this for making the things fast..

Is that possible??

Thanks

If you know the values, you can do something like this.

insert into table (col1) values (val1),(val2),(val3)

This will insert 3 records to the table.

nave it is ok when we enter the records manually and we know the no. of records

Here it is not the case... I have an array having the value of several records that i it got as a result of a query..

I now want to insert these records into an another table without using the while or for loop...
Hope u get me..

Thanks in advance..
Jino.

Ah ! Then, *I think* you need a loop to do that.

I am trying to do that without a loop...

cos that will make things done faster..

Pls get back to me if u get any idea about this..

Once again thx
Jino.

If you use the mysqli interface, you can concat all queries into a single string, separated by semi-colons. Then all will be executed after one another. The error-handling will be more difficult though. See here:
http://nl.php.net/manual/en/mysqli.multi-query.php

I think you can get it done relatively easy by using INSERT ... SELECT.
This is the example found in MySQL Reference Manual:

INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.