Bulk Insertion with PHP – MySQL
by Gopakumar on Mar.10, 2009, under Fire Key
Many of my pals asked me about inserting a bulk SQL query with hundreds of lines to a mysql database using php. As many of you know that mysql_query() function in PHP is not capable of handling semicolon seperated multi-line queries. It can only handle one query at a time. For more information on PHP mysql_query() function please visit PHP.net mysql_query() function page. In normal case a PHP query will be like this :
1 2 3 4 | <?PHP // Query to select an int column $query = 'SELECT user_id FROM users WHERE user_id = 1'; ?> |
But we need to insert some bulk sql statements. What should we do?
We can do it in two ways. The first is quering from a variable that contains a bulk sql statements which is semi-colon seperated and the second one is an external file that contains the sql statements. First take a look at the variable that contains the sql statements.
$READ ="CREATE TABLE IF NOT EXISTS `gnet_content` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL DEFAULT '', `alias` varchar(255) NOT NULL DEFAULT '', `title_alias` varchar(255) NOT NULL DEFAULT '', `introtext` mediumtext NOT NULL, `fulltext` mediumtext NOT NULL, `state` tinyint(3) NOT NULL DEFAULT '0', `sectionid` int(11) unsigned NOT NULL DEFAULT '0', `mask` int(11) unsigned NOT NULL DEFAULT '0', `catid` int(11) unsigned NOT NULL DEFAULT '0', `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `created_by` int(11) unsigned NOT NULL DEFAULT '0', `created_by_alias` varchar(255) NOT NULL DEFAULT '', `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `modified_by` int(11) unsigned NOT NULL DEFAULT '0', `checked_out` int(11) unsigned NOT NULL DEFAULT '0', `checked_out_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `publish_up` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `publish_down` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `images` text NOT NULL, `urls` text NOT NULL, `attribs` text NOT NULL, `version` int(11) unsigned NOT NULL DEFAULT '1', `parentid` int(11) unsigned NOT NULL DEFAULT '0', `ordering` int(11) NOT NULL DEFAULT '0', `metakey` text NOT NULL, `metadesc` text NOT NULL, `access` int(11) unsigned NOT NULL DEFAULT '0', `hits` int(11) unsigned NOT NULL DEFAULT '0', `metadata` text NOT NULL, PRIMARY KEY (`id`), KEY `idx_section` (`sectionid`), KEY `idx_access` (`access`), KEY `idx_checkout` (`checked_out`), KEY `idx_state` (`state`), KEY `idx_catid` (`catid`), KEY `idx_createdby` (`created_by`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=46 ; CREATE TABLE IF NOT EXISTS `gnet_polls` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL DEFAULT '', `alias` varchar(255) NOT NULL DEFAULT '', `voters` int(9) NOT NULL DEFAULT '0', `checked_out` int(11) NOT NULL DEFAULT '0', `checked_out_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `published` tinyint(1) NOT NULL DEFAULT '0', `access` int(11) NOT NULL DEFAULT '0', `lag` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ; CREATE TABLE IF NOT EXISTS `gnet_users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT '', `username` varchar(150) NOT NULL DEFAULT '', `email` varchar(100) NOT NULL DEFAULT '', `password` varchar(100) NOT NULL DEFAULT '', `usertype` varchar(25) NOT NULL DEFAULT '', `block` tinyint(4) NOT NULL DEFAULT '0', `sendEmail` tinyint(4) DEFAULT '0', `gid` tinyint(3) unsigned NOT NULL DEFAULT '1', `registerDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `lastvisitDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `activation` varchar(100) NOT NULL DEFAULT '', `params` text NOT NULL, PRIMARY KEY (`id`), KEY `usertype` (`usertype`), KEY `idx_name` (`name`), KEY `gid_block` (`gid`,`block`), KEY `username` (`username`), KEY `email` (`email`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=64 ; CREATE TABLE IF NOT EXISTS `gnet_weblinks` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `catid` int(11) NOT NULL DEFAULT '0', `sid` int(11) NOT NULL DEFAULT '0', `title` varchar(250) NOT NULL DEFAULT '', `alias` varchar(255) NOT NULL DEFAULT '', `url` varchar(250) NOT NULL DEFAULT '', `description` text NOT NULL, `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `hits` int(11) NOT NULL DEFAULT '0', `published` tinyint(1) NOT NULL DEFAULT '0', `checked_out` int(11) NOT NULL DEFAULT '0', `checked_out_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `ordering` int(11) NOT NULL DEFAULT '0', `archived` tinyint(1) NOT NULL DEFAULT '0', `approved` tinyint(1) NOT NULL DEFAULT '1', `params` text NOT NULL, PRIMARY KEY (`id`), KEY `catid` (`catid`,`published`,`archived`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;";
In the second method, consider we have a file named mydatabase.sql that contains the same table creation scripts and initial data for our new php application. And you have to run insert this in the first run. We will first reads the file to a php variable with the code below :
$FP = fopen ( 'mydatabase.sql', 'r' ); $READ = fread ( $FP, filesize ( 'mydatabase.sql') );
Now we need to query the code. we use the following script to execute the query.
$db = mysql_connect ( 'localhost', 'username', 'password' ); mysql_select_db( 'database', $db ); $READ = explode ( ";\n", $READ ); foreach ( $READ AS $RED ) { mysql_query ( $RED ); }
In the code above, we just explode the string with “;\n” (semi-colon and new-line) as delimiter. And we got an array which contains each query as an element. With a for each loop we execute all queries that contained in the $READ array.
I hope this tutorial was useful for you.













February 14th, 2010 on 3:06 am
I am to a great extent impressed with the article I have just read. I wish the writer of gopan.artxmagic.com can continue to provide so much useful information and unforgettable experience to gopan.artxmagic.com readers. There is not much to state except the following universal truth: 30 seconds after permanently deleting/throwing/burning it, you need it. I will be back.
October 28th, 2009 on 6:54 am
Hey,
Just wanted to thank you for putting this script up, helped me a similar problem i was having.
Cheers,
Turk
October 2nd, 2009 on 10:09 am
Seems like you really took your time on this. Keep up the good work!
September 27th, 2009 on 11:22 am
Really great blog here. Thanks!
September 26th, 2009 on 12:39 pm
Thank you for a great blog, I will be sure to bookmark your site and check back later
September 22nd, 2009 on 12:37 pm
Thank you for a great blog, I will be sure to bookmark your site and check back later
September 17th, 2009 on 9:20 am
Thank you for a great blog.
September 15th, 2009 on 6:55 am
I don’t usually post but I enjoyed your blog a lot.
September 13th, 2009 on 4:09 am
Looks like your doing a good job with this blog.
September 9th, 2009 on 2:58 am
Thank you for a great blog.
September 8th, 2009 on 9:36 am
Usually I don’t leave a comment but I wanted to let you know that I really like your site.
September 7th, 2009 on 5:59 pm
I don’t usually post but I enjoyed your blog a lot,Thanks alot for the great read
September 7th, 2009 on 4:05 pm
I don’t usually post but I enjoyed your blog a lot,Thanks alot for the great read