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? Link to comment https://forums.phpfreaks.com/topic/118192-solved-php-sql-statements-s-or-values/ 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. Link to comment https://forums.phpfreaks.com/topic/118192-solved-php-sql-statements-s-or-values/#findComment-608269 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. Link to comment https://forums.phpfreaks.com/topic/118192-solved-php-sql-statements-s-or-values/#findComment-609564 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? Link to comment https://forums.phpfreaks.com/topic/118192-solved-php-sql-statements-s-or-values/#findComment-610091 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. Link to comment https://forums.phpfreaks.com/topic/118192-solved-php-sql-statements-s-or-values/#findComment-610116 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. Link to comment https://forums.phpfreaks.com/topic/118192-solved-php-sql-statements-s-or-values/#findComment-610410 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. Link to comment https://forums.phpfreaks.com/topic/118192-solved-php-sql-statements-s-or-values/#findComment-610744 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.