471,354 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Multiple insertion querry

Hello,
I have an array of data in PHP. I would like to insert each member of the
array into it's own row in SQL. The array is of variable length, so it
would have to be dynamic code. How would I go about this? Would I stick
the SQL querry generation and actual querry into a while loop? This would
generate a lot of traffic between the SQL server and the PHP script.

The arrays are each over 1000 members long, however, this is an rare admin
script that will be run roughly 4 times a year, so I guess it isn't that
bad. If possible however, I would like to do it in one querry. Is that
possible? I thought I heard somewhere that it's impossible to run more
that one querry at once through PHP as a failsafe for users sticking bad
data into forms. (ie, you cant do: "querry1; querry2;", you have to do
"querry1;" querry it, then do "querry2;"

Thanks in advance,
-Eric Kincl
Jul 17 '05 #1
2 3081
Eric Kincl wrote:
[...] you cant do: "querry1; querry2;", you have to do
"querry1;" querry it, then do "querry2;"


with MySQL you can do a insert with multiple rows in a single query:

INSERT <table> [(col1, col2, ...)]
VALUES (val11, val12, ...),
(val21, val22, ...),
...
(valN1, valN2, ...)
HTH
PS. I'm more used to writing query :)

--
..sig
Jul 17 '05 #2
Eric Kincl <Er**@Kincl.net_NO_SPAM_> wrote
in message news:<3f******@news.gvsu.edu>...

I have an array of data in PHP. I would like to insert each
member of the array into it's own row in SQL. The array is
of variable length, so it would have to be dynamic code.
How would I go about this?


By compiling one hell of a long query and executing it at once.
This is much faster than any solution with multiple INSERTs.

$array = array ('Many', 'many', 'many', 'members');
$query = "INSERT INTO yourtable (yourcolumn) VALUES (('" .
implode ("'), ('", $array) .
"'))";
$result = mysql_query ($query);

If I put in the right punctuation, the variable $query must
contain:

INSERT INTO yourtable (yourcolumn)
VALUES (('Many'), ('many'), ('many'), ('members'))

Cheers,
NC
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Raju V.K | last post: by
reply views Thread by Costa Lino | last post: by
20 posts views Thread by Patrick Guio | last post: by
reply views Thread by gabe_pu | last post: by
5 posts views Thread by Clownfish | last post: by
9 posts views Thread by toton | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.