mysql_insert_id

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

    mysql_insert_id

    how could i reduce the following piece of code:

    for ($num=0;$num<50 ;$num++) {
    mysql_query($db ,"INSERT INTO whatever (stuff) VALUES (somevalue);";
    mysql_query($db ,"INSERT INTO sometable VALUES
    (".mysql_insert _id().");";
    }

    to something where only two sql queries are made (as opposed to 100?).
    ie. where the first line could be converted to something like this:

    mysql_query($db ,"INSERT INTO whatever (stuff) VALUES
    (".implode(",", somearray).")";

    where somearray contains every somevalue.
  • Alan Little

    #2
    Re: mysql_insert_id

    Carved in mystic runes upon the very living rock, the last words of
    yawnmoth of comp.lang.php make plain:
    [color=blue]
    > how could i reduce the following piece of code:
    >
    > for ($num=0;$num<50 ;$num++) {
    > mysql_query($db ,"INSERT INTO whatever (stuff) VALUES (somevalue);";
    > mysql_query($db ,"INSERT INTO sometable VALUES
    > (".mysql_insert _id().");";
    > }
    >[/color]

    I don't know if you're aware of extended insert format:

    insert into Table (f1, f2) values (v1a, v2a), (v1b, v2b)....

    However, mysql_insert_id () will only return the value for the first
    record. What you could do, though, is this:

    $FirstID = mysql_insert_id ();
    $Query = "select ID from Table where ID>=$FirstID limit 0, $NumRows";

    Where $NumRows is the number of rows you inserted. Since all the rows
    were inserted by one query, they will have consecutive IDs.

    --
    Alan Little
    Phorm PHP Form Processor

    Comment

    Working...