Multiple insertion querry

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Kincl

    #1

    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
  • Pedro Graca

    #2
    Re: Multiple insertion querry

    Eric Kincl wrote:[color=blue]
    > [...] you cant do: "querry1; querry2;", you have to do
    > "querry1;" querry it, then do "querry2;"[/color]

    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

    Comment

    • Nikolai Chuvakhin

      #3
      Re: Multiple insertion querry

      Eric Kincl <Eric@Kincl.net _NO_SPAM_> wrote
      in message news:<3fbbcc1f@ news.gvsu.edu>. ..[color=blue]
      >
      > 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?[/color]

      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

      Comment

      Working...