By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

Multiple insertion querry

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.