#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,671
    Rep Power
    171

    Php Loop Sql Insert


    Does this mean if you are using PHP with a database system (which is not DB2, PostgreSQL or MySQL), it is ok to put the insert query in the loop no matter how large the loop is?
    Where one INSERT statement inserts multiple rows of data, is valid SQL, not every database system allows the INSERT statement to use multiple row constructors; those that do allow it include DB2, PostgreSQL, and MySQL. If your database systemís INSERT statement allows only one row to be inserted at a time, as is the case with SQL Server, simply run three INSERT statements.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    Considering how there aren't any alternatives, yes.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi zxcvbnm,

    that statement hardly makes sense. Some database systems added row value constructors later or don't have them at all (like Oracle), but you could always do a UNION to insert multiple rows in a single query.

    Nowadays, even sqlite has row value constructors. The only exception is the already mentioned Oracle and maybe some of the exotic systems.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,671
    Rep Power
    171
    Originally Posted by Jacques1
    Hi zxcvbnm,

    that statement hardly makes sense. Some database systems added row value constructors later or don't have them at all (like Oracle), but you could always do a UNION to insert multiple rows in a single query.

    Nowadays, even sqlite has row value constructors. The only exception is the already mentioned Oracle and maybe some of the exotic systems.
    Hello Jacques1;

    That statement is from chapter one of Rudy's SimplySql. Maybe I copy pasted wrong.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by zxcvbnm
    Maybe I copy pasted wrong.
    No, it is wrong. Even before 2008, when SQL Server didn't have row value constructors, you could simply do multiple UNION ALL:

    Code:
    INSERT INTO your_table (foo, bar)
    SELECT 'abc', 'def'
    UNION ALL
    SELECT 'ghi', 'jkl'
    ;
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,204
    Rep Power
    4279
    Originally Posted by Jacques1
    No, it is wrong.
    wrong is such a harsh word



    Originally Posted by da book
    If your database systemís INSERT statement allows only one row to be inserted at a time, as is the case with SQL Server, simply run three INSERT statements
    UNION will also work, but at this point in the book, i didn't want to take a detour and explain how you can sometimes select "out of thin air" (i.e. without a FROM clause), which is also wrong (non-standard)

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    nice to share the information

IMN logo majestic logo threadwatch logo seochat tools logo