Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, October 14, 2016

Insert thousands of records in database using PHP

Insert thousands of records in database using PHP


I have this kind of data being generated:

INSERT INTO `mytable` (`idSample`, `postSampeId`, `title`, `link`, `description`, `date`, `views`, `clicks`, `isDeleted`) VALUES      (41021, 121, 'Job Board Detox Tip #9: Attend a Networking Event for Non-Job Seekers', 'http://www.careerealism.com/job-board-detox-networking-event/', '\nToday?sjob board detoxtip: Attend a networking event for non-job seekers.\nYes, your challenge today is to attend a networking event that is NOT targeted to job seekers. Why? Because it will help you to meet the target audience you ', '16 Aug 2012, 12:30 am', 523, 0, 0),      (41026, 78, 'Olympic Sponsors Go For Gold Online [INFOGRAPHIC]', 'http://feeds.mashable.com/~r/Mashable/SocialMedia/~3/MPop6ENLC94/', '\n\n\n\nThe Olympics are over, and whether you were watching or not, you probably couldn''t avoid their sponsors'' ads. \nUnmetric, a service that tracks brands'' online presence, collected data leading up to the Games to see how sponsors stacked up against ', '16 Aug 2012, 12:58 am', 410, 0, 0),      (41025, 75, 'Updated Google Play app reveals gift card support, wish list coming soon?', 'http://www.engadget.com/2012/08/16/google-play-wishlist-gift-card-leak/', '\n \n If the images and link settings Android Police has dug up from the most recent (v3.8.15) Google Play APK that''s rolling out are correct, we could see a coupe more features coming to the Android app and media store soon. First up are very fleshed ', '16 Aug 2012, 12:14 am', 436, 0, 0),      (41028, 95, 'The Gadgets that Got Us Going, Back in 2002 [Gizmodo10]', 'http://feeds.gawker.com/~r/gizmodo/full/~3/9K9bDZpH58g/the-gadgets-the-got-us-going-back-in-2002', '\n\n                                                         \n                      \n                                  \n              Gizmodo is officially 10 years old! And, while we''ve been celebrating this all week, I thought it would be fun (and/or funny) to take a look back at the ancestor gadgets we would all be using if someone were to ', '16 Aug 2012, 12:00 am', 402, 0, 0),      (41029, 95, 'US Suffers Another Blow in Its Case Against Kim Dotcom [Kim Dotcom]', 'http://feeds.gawker.com/~r/gizmodo/full/~3/RVKHECRt2eI/us-suffers-another-blow-in-its-case-against-kim-dotcom', '\n\n                                                            \n                      \n                                  \n              Is it okay for me to say that I''m a little glad we''re doing so poorly in our case against Megaupload kingpin Kim Dotcom? I am. I am so glad. I miss Megaupload dearly and I''m truly pleased by this latest news: Ju', '16 Aug 2012, 1:00 am', 402, 0, 0),      (41032, 66, 'Hanwha Chairman Gets 4-Year Sentence', 'http://online.wsj.com/article/SB10000872396390444508504577592203136561574.html?mod=pls_whats_news_us_business_f', 'A South Korean court sentenced the leader of one of the country''s largest business groups, Hanwha Group, to four years in prison for using corporate funds to pay his own debts. The group plans to appeal the sentence.', '16 Aug 2012, 1:21 am', 523, 0, 0),      (41031, 98, 'Sunglass Holders, iCloud Dropbox, and Cable Storage [From The Tips Box]', 'http://feeds.gawker.com/~r/lifehacker/full/~3/QakmYYUdVck/', '\n\n                                                           \n                      \n                                  \n              Readers offer their best tips for storing your sunglasses in your car, making iCloud work like Dropbox, and storing your cables on-the-go.              More?\n             \n\n   \n\n\n', '15 Aug 2012, 7:00 pm', 426, 0, 0),      (41041, 77, 'Dynamic Solar Gets NY-Sun Funding', 'http://feeds.importantmedia.org/~r/IM-cleantechnica/~3/ew8TEuGaQX4/', '\nDynamic Solarwas one of the lucky 16 recipients of the $30 million New York (NY)-Sun award on Wednesday.\nThe purpose of the NY-Sun program is to help businesses and other organizations get electricity from solar energy sources.\n\nThe awar', '16 Aug 2012, 2:01 am', 233, 0, 0),      (41042, 68, 'Taylor Swift -- Crazy for Break-Up Songs', 'http://www.tmz.com/2012/08/16/taylor-swift-conor-kennedy-we-are-never-ever-getting-back-together-tmz-tv/', 'Taylor Swift has a habit of trashing exes in song ... including her new #1 hit -- so who''s crazier? Taylor or the dudes who keep dating her? Harvey is definitely leaning one way on this.Check out TMZ on TV -- click here to see your local listings!\n', '16 Aug 2012, 2:02 am', 231, 0, 0),      (41035, 121, 'Ask Our Experts: What?s the Hardest Interview Question and How Do You Answer it?', 'http://www.careerealism.com/hardest-interview-questions-ask-our-experts/', '\nEach week, we ask our experts to answer acareerquestion on behalf of our readers.\nThis week?s question is about thehardest interview questions:\n?What?s the hardest interview question to answer and what tips can yo', '16 Aug 2012, 1:00 am', 357, 0, 0),      (41036, 121, 'Working as a Mechanic: No Degree, No Problem', 'http://www.careerealism.com/working-mechanic/', '\nThis is a true story as told to DiversityJobs Street Smarts, where you can find career interviews for the job you?ve been looking for.Visit to find an interview in your desired field today.\nI am a mechanic at Walls Garage in Greenw', '16 Aug 2012, 1:30 am', 357, 0, 0),      (41039, 75, 'Acer CloudMobile now available for pre-order in the UK, expected to be released on September 5th', 'http://www.engadget.com/2012/08/16/acer-cloudmobile-smartphone-pre-orders/', '\n \n Acer took the gadget world by surprise with its award-winning CloudMobile handset, and now it looks like the company''s finally ready to let consumers get their eager palms on that 4.3-inch, 720p slab. Per online retailer Expansys, Acer''s CloudMob', '16 Aug 2012, 1:00 am', 293, 0, 0),      (41040, 75, 'Fujitsu''s Stylistic M532 quad-core tablet ships to the US with toughened body, dash of security', 'http://www.engadget.com/2012/08/16/fujitsu-stylistic-m532-quad-core-tablet-ships-to-the-us/', '\n \n Fujitsu''s Stylistic M532 has had a protracted development process that saw it appear on our radar as early as January; imagine our surprise after it ships to the US with barely more than a knock on the door. Now that it''s here, it looks to be on ', '16 Aug 2012, 1:39 am', 311, 0, 0),      (41045, 42, 'Ex-Wife Sues Yogurt Mogul', 'http://www.inc.com/maeghan-ouimet/exwife-sues-chobani-ceo.html', '\nThe hottest brand in yogurt is in the center of a battle over who owns a majority stake in the company.\nHamdi Ulukaya, owner of Greek yogurt company Chobani, received sour news Tuesday: According to the New York Post, his ex-wife Ayse Giray has file', '16 Aug 2012, 1:00 am', 190, 0, 0),      (41044, 68, 'Marion Jones -- Sells Tiny TX House ... Buys Not-So-Tiny House', 'http://www.tmz.com/2012/08/16/marion-jones-texas-home-gallery/', 'Things are looking up for Marion Jones -- who was infamously stripped of her Olympic gold medals -- because she just traded in her humble Texas home ... for a much nicer version. In case you didn''t already know ... in 2007 Marion -- who was?\n', '16 Aug 2012, 2:15 am', 220, 0, 0),      (41051, 47, 'Celeb birthdays for the week of Aug. 19-25', 'http://news.yahoo.com/celeb-birthdays-week-aug-19-25-050243812.html', 'Aug. 19: Actress Diana Muldaur ("Star Trek: The Next Generation") is 74. Drummer Ginger Baker (Cream, Blind Faith) is 73. Singer Johnny Nash is 72. Actress Jill St. John is 72. Singer Billy J. Kramer is 69. Country singer-songwriter Eddy Raven is 68.', '16 Aug 2012, 12:02 am', 119, 0, 0),      (41052, 47, 'Kuwait drama on war unity contrasts current feuds', 'http://news.yahoo.com/kuwait-drama-war-unity-contrasts-current-feuds-071008420.html', '\nEach night for the past three weeks, families in Kuwait have been transfixed by a drama in which they already know the ending: Iraq forces will be driven out and the shattered Gulf nation will rebuild. But a 30-part television serial on Iraq''s 1990 ', '16 Aug 2012, 2:11 am', 119, 0, 0),      (41049, 76, 'Because That?s Where The Content Is', 'http://feedproxy.google.com/~r/Techcrunch/~3/_RH5C6yRbqg/', '\nWhen asked why he robbed banks, legend has it that Willie Sutton said, ?because that?s where the money is.?\nI?m reminded of this quote today when reading the reports that Apple is in talks with the cable operators about their', '16 Aug 2012, 2:32 am', 136, 0, 0),      (41047, 75, 'Kobo app for iOS goes international with support for Dutch, French, German, Italian and Spanish', 'http://www.engadget.com/2012/08/16/kobo-app-for-ios-gains-international-support/', '\n \n Kobo''s Android app recently enjoyed a brief stint of global superiority, but that playing field has been leveled with the company''s latest update for iOS. Like the Android version, Kobo for iOS now includes support for Dutch, French, German, Ital', '16 Aug 2012, 2:21 am', 156, 0, 0),      (41050, 95, 'Dish Network Is Planning Nationwide Satellite Broadband [Rumors]', 'http://feeds.gawker.com/~r/gizmodo/full/~3/OrDCXmlu0oA/dish-network-is-planning-nationwide-satellite-broadband', '\n\n                                                          \n                      \n                                  \n              People living in the sticks, rejoice! Bloomberg is reporting that Dish Network is planning to roll out a nationwide satellite broadband internet service ?and promises speeds of between 5 and 15 megabits pe', '16 Aug 2012, 2:47 am', 138, 0, 0);  

I wanted to run this one using PHP. I know that one way to do this one is like this:

$str = "  INSERT INTO `mytable` (`idSample`, `postSampeId`, `title`, `link`, `description`, `date`, `views`, `clicks`, `isDeleted`) VALUES          (41021, 121, 'Job Board Detox Tip #9: Attend a Networking Event for Non-Job Seekers', 'http://www.careerealism.com/job-board-detox-networking-event/', '\nToday?sjob board detoxtip: Attend a networking event for non-job seekers.\nYes, your challenge today is to attend a networking event that is NOT targeted to job seekers. Why? Because it will help you to meet the target audience you ', '16 Aug 2012, 12:30 am', 523, 0, 0),          (41026, 78, 'Olympic Sponsors Go For Gold Online [INFOGRAPHIC]', 'http://feeds.mashable.com/~r/Mashable/SocialMedia/~3/MPop6ENLC94/', '\n\n\n\nThe Olympics are over, and whether you were watching or not, you probably couldn''t avoid their sponsors'' ads. \nUnmetric, a service that tracks brands'' online presence, collected data leading up to the Games to see how sponsors stacked up against ', '16 Aug 2012, 12:58 am', 410, 0, 0),          (41025, 75, 'Updated Google Play app reveals gift card support, wish list coming soon?', 'http://www.engadget.com/2012/08/16/google-play-wishlist-gift-card-leak/', '\n \n If the images and link settings Android Police has dug up from the most recent (v3.8.15) Google Play APK that''s rolling out are correct, we could see a coupe more features coming to the Android app and media store soon. First up are very fleshed ', '16 Aug 2012, 12:14 am', 436, 0, 0),          (41028, 95, 'The Gadgets that Got Us Going, Back in 2002 [Gizmodo10]', 'http://feeds.gawker.com/~r/gizmodo/full/~3/9K9bDZpH58g/the-gadgets-the-got-us-going-back-in-2002', '\n\n                                                         \n                      \n                                  \n              Gizmodo is officially 10 years old! And, while we''ve been celebrating this all week, I thought it would be fun (and/or funny) to take a look back at the ancestor gadgets we would all be using if someone were to ', '16 Aug 2012, 12:00 am', 402, 0, 0),          (41029, 95, 'US Suffers Another Blow in Its Case Against Kim Dotcom [Kim Dotcom]', 'http://feeds.gawker.com/~r/gizmodo/full/~3/RVKHECRt2eI/us-suffers-another-blow-in-its-case-against-kim-dotcom', '\n\n                                                            \n                      \n                                  \n              Is it okay for me to say that I''m a little glad we''re doing so poorly in our case against Megaupload kingpin Kim Dotcom? I am. I am so glad. I miss Megaupload dearly and I''m truly pleased by this latest news: Ju', '16 Aug 2012, 1:00 am', 402, 0, 0),          (41032, 66, 'Hanwha Chairman Gets 4-Year Sentence', 'http://online.wsj.com/article/SB10000872396390444508504577592203136561574.html?mod=pls_whats_news_us_business_f', 'A South Korean court sentenced the leader of one of the country''s largest business groups, Hanwha Group, to four years in prison for using corporate funds to pay his own debts. The group plans to appeal the sentence.', '16 Aug 2012, 1:21 am', 523, 0, 0),          (41031, 98, 'Sunglass Holders, iCloud Dropbox, and Cable Storage [From The Tips Box]', 'http://feeds.gawker.com/~r/lifehacker/full/~3/QakmYYUdVck/', '\n\n                                                           \n                      \n                                  \n              Readers offer their best tips for storing your sunglasses in your car, making iCloud work like Dropbox, and storing your cables on-the-go.              More?\n             \n\n   \n\n\n', '15 Aug 2012, 7:00 pm', 426, 0, 0),          (41041, 77, 'Dynamic Solar Gets NY-Sun Funding', 'http://feeds.importantmedia.org/~r/IM-cleantechnica/~3/ew8TEuGaQX4/', '\nDynamic Solarwas one of the lucky 16 recipients of the $30 million New York (NY)-Sun award on Wednesday.\nThe purpose of the NY-Sun program is to help businesses and other organizations get electricity from solar energy sources.\n\nThe awar', '16 Aug 2012, 2:01 am', 233, 0, 0),          (41042, 68, 'Taylor Swift -- Crazy for Break-Up Songs', 'http://www.tmz.com/2012/08/16/taylor-swift-conor-kennedy-we-are-never-ever-getting-back-together-tmz-tv/', 'Taylor Swift has a habit of trashing exes in song ... including her new #1 hit -- so who''s crazier? Taylor or the dudes who keep dating her? Harvey is definitely leaning one way on this.Check out TMZ on TV -- click here to see your local listings!\n', '16 Aug 2012, 2:02 am', 231, 0, 0),          (41035, 121, 'Ask Our Experts: What?s the Hardest Interview Question and How Do You Answer it?', 'http://www.careerealism.com/hardest-interview-questions-ask-our-experts/', '\nEach week, we ask our experts to answer acareerquestion on behalf of our readers.\nThis week?s question is about thehardest interview questions:\n?What?s the hardest interview question to answer and what tips can yo', '16 Aug 2012, 1:00 am', 357, 0, 0),          (41036, 121, 'Working as a Mechanic: No Degree, No Problem', 'http://www.careerealism.com/working-mechanic/', '\nThis is a true story as told to DiversityJobs Street Smarts, where you can find career interviews for the job you?ve been looking for.Visit to find an interview in your desired field today.\nI am a mechanic at Walls Garage in Greenw', '16 Aug 2012, 1:30 am', 357, 0, 0),          (41039, 75, 'Acer CloudMobile now available for pre-order in the UK, expected to be released on September 5th', 'http://www.engadget.com/2012/08/16/acer-cloudmobile-smartphone-pre-orders/', '\n \n Acer took the gadget world by surprise with its award-winning CloudMobile handset, and now it looks like the company''s finally ready to let consumers get their eager palms on that 4.3-inch, 720p slab. Per online retailer Expansys, Acer''s CloudMob', '16 Aug 2012, 1:00 am', 293, 0, 0),          (41040, 75, 'Fujitsu''s Stylistic M532 quad-core tablet ships to the US with toughened body, dash of security', 'http://www.engadget.com/2012/08/16/fujitsu-stylistic-m532-quad-core-tablet-ships-to-the-us/', '\n \n Fujitsu''s Stylistic M532 has had a protracted development process that saw it appear on our radar as early as January; imagine our surprise after it ships to the US with barely more than a knock on the door. Now that it''s here, it looks to be on ', '16 Aug 2012, 1:39 am', 311, 0, 0),          (41045, 42, 'Ex-Wife Sues Yogurt Mogul', 'http://www.inc.com/maeghan-ouimet/exwife-sues-chobani-ceo.html', '\nThe hottest brand in yogurt is in the center of a battle over who owns a majority stake in the company.\nHamdi Ulukaya, owner of Greek yogurt company Chobani, received sour news Tuesday: According to the New York Post, his ex-wife Ayse Giray has file', '16 Aug 2012, 1:00 am', 190, 0, 0),          (41044, 68, 'Marion Jones -- Sells Tiny TX House ... Buys Not-So-Tiny House', 'http://www.tmz.com/2012/08/16/marion-jones-texas-home-gallery/', 'Things are looking up for Marion Jones -- who was infamously stripped of her Olympic gold medals -- because she just traded in her humble Texas home ... for a much nicer version. In case you didn''t already know ... in 2007 Marion -- who was?\n', '16 Aug 2012, 2:15 am', 220, 0, 0),          (41051, 47, 'Celeb birthdays for the week of Aug. 19-25', 'http://news.yahoo.com/celeb-birthdays-week-aug-19-25-050243812.html', 'Aug. 19: Actress Diana Muldaur ("Star Trek: The Next Generation") is 74. Drummer Ginger Baker (Cream, Blind Faith) is 73. Singer Johnny Nash is 72. Actress Jill St. John is 72. Singer Billy J. Kramer is 69. Country singer-songwriter Eddy Raven is 68.', '16 Aug 2012, 12:02 am', 119, 0, 0),          (41052, 47, 'Kuwait drama on war unity contrasts current feuds', 'http://news.yahoo.com/kuwait-drama-war-unity-contrasts-current-feuds-071008420.html', '\nEach night for the past three weeks, families in Kuwait have been transfixed by a drama in which they already know the ending: Iraq forces will be driven out and the shattered Gulf nation will rebuild. But a 30-part television serial on Iraq''s 1990 ', '16 Aug 2012, 2:11 am', 119, 0, 0),          (41049, 76, 'Because That?s Where The Content Is', 'http://feedproxy.google.com/~r/Techcrunch/~3/_RH5C6yRbqg/', '\nWhen asked why he robbed banks, legend has it that Willie Sutton said, ?because that?s where the money is.?\nI?m reminded of this quote today when reading the reports that Apple is in talks with the cable operators about their', '16 Aug 2012, 2:32 am', 136, 0, 0),          (41047, 75, 'Kobo app for iOS goes international with support for Dutch, French, German, Italian and Spanish', 'http://www.engadget.com/2012/08/16/kobo-app-for-ios-gains-international-support/', '\n \n Kobo''s Android app recently enjoyed a brief stint of global superiority, but that playing field has been leveled with the company''s latest update for iOS. Like the Android version, Kobo for iOS now includes support for Dutch, French, German, Ital', '16 Aug 2012, 2:21 am', 156, 0, 0),          (41050, 95, 'Dish Network Is Planning Nationwide Satellite Broadband [Rumors]', 'http://feeds.gawker.com/~r/gizmodo/full/~3/OrDCXmlu0oA/dish-network-is-planning-nationwide-satellite-broadband', '\n\n                                                          \n                      \n                                  \n              People living in the sticks, rejoice! Bloomberg is reporting that Dish Network is planning to roll out a nationwide satellite broadband internet service ?and promises speeds of between 5 and 15 megabits pe', '16 Aug 2012, 2:47 am', 138, 0, 0);  ";    mysql_query( $str ) or die( mysql_error() );  

But the problem here is that some of the words/sentences here have double quotes and some have single quotes, but I don't want to replace that one(like replace single into double quotes, vice versa). How would I do that one? Your help would be greatly appreciated and rewarded.

Thanks! :)

PS: This data was generated when I export that in phpmyadmin.I don't want to use phpmyadmin to insert/import those data. What I wanted to use is a php code without modifying anything that the phpmyadmin export result.

Answer by Nelson for Insert thousands of records in database using PHP


You don't have to replace them, you have to escape them.

Answer by JP. for Insert thousands of records in database using PHP


You created your string $str with ", so you would have to escape " into \".

Another solution would be to have the generated SQL in a file and use the SOURCE command. The SQL query to execute would look like this:

SOURCE filename.sql  

Answer by Mona Abdelmajeed for Insert thousands of records in database using PHP


Try to use

1 - Addslashes Whitch add slashe on every quote in str

2 - mysql_real_escape_string escape and ignore quote

But As advice Try to Cut This Query to more than one

Because : thousand of insert can kill the database

Answer by user1003932 for Insert thousands of records in database using PHP


You don't want to automate mysql dump files, especially from phpmyadmin. phpmyadmin can produce sql dump code with errors in it. You really need to quality check mysql dump code. Once you have a proven dump process, your best bet it to write a bash dump script that you can execute from php.

Answer by JM Hicks for Insert thousands of records in database using PHP


This php page on string literals discusses a syntax called a heredoc. http://php.net/manual/en/language.types.string.php

The heredoc defined string works like a php double-quote string literal, except it's not delimited by double-quotes but by a user-defined token. So, it doesn't have the problem with misinterpreting an internal double-quote as the end of your php string value.

One of the example heredoc string literals given in the above linked page is something like this:

$str = <<

In that case you could replace the

these are my insert statements  

with your actual statement text.

This syntax would eliminate the errors due to misinterpretation of the double-quote-" characters.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.