xtopolis Posted August 5, 2008 Share Posted August 5, 2008 Hi, Is there a reason for using one of the methods to form SQL statements over another? The two I know are: $sql = "INSERT INTO table SET COL1='$col1', COL2='$col2'"; vs $sql = "INSERT INTO table (COL1, COL2) VALUES ('$col1', '$col2')"; Any reason, or are they just personal preference? Quote Link to comment Share on other sites More sharing options...
revraz Posted August 5, 2008 Share Posted August 5, 2008 For those two examples, personal preference. They both will do the same thing. I would say the 2nd example is more widely used. Quote Link to comment Share on other sites More sharing options...
fenway Posted August 6, 2008 Share Posted August 6, 2008 The 2nd method also makes it possible to use multi-valued insert statements. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted August 6, 2008 Author Share Posted August 6, 2008 I use the first way, just since that's how I learned it, seems more straight forward in my mind. What do you mean by a multivalued insert statement? Quote Link to comment Share on other sites More sharing options...
fenway Posted August 6, 2008 Share Posted August 6, 2008 You can used many VALUES() one after the other to insert N multiple rows... assuming you don't need the IDs of the newly created rows back, it's much faster than running N insert statements. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted August 7, 2008 Author Share Posted August 7, 2008 Ah, took me some searching but I see what you mean. Thanks for the tip, didn't know I could just do: <?php $sql = "INSERT INTO table (col,col2,col3) VALUES ('1','2','3'),('4','5','6')"; ?> for multiple rows. Thanks. Quote Link to comment Share on other sites More sharing options...
fenway Posted August 7, 2008 Share Posted August 7, 2008 Performance-wise, it's a substantial improvement if you're inserting many rows. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.