Imorting database tables from a sql file using php mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
basudeb
Forum Commoner
Posts: 44
Joined: Wed Dec 20, 2006 12:13 am

Imorting database tables from a sql file using php mysql

Post by basudeb »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a .sql file containing the exporting of a database. I want to import this sql file using php script.
Suppose the database name is TESTDB and the sql file contains the following:


[syntax="sql"]-- phpMyAdmin SQL Dump 
-- version 2.6.1 
-- http://www.phpmyadmin.net 
-- 
-- Host: localhost 
-- Generation Time: Dec 20, 2006 at 12:35 PM 
-- Server version: 4.1.9 
-- PHP Version: 4.3.10 
-- 
-- Database: `community` 
-- 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `community_admin` 
-- 

CREATE TABLE `community_admin` ( 
`adminId` int(10) NOT NULL auto_increment, 
`username` varchar(200) NOT NULL default '', 
`password` varchar(200) NOT NULL default '', 
`name` varchar(200) NOT NULL default '', 
`date` date NOT NULL default '0000-00-00', 
`access` char(1) NOT NULL default '', 
`status` char(1) NOT NULL default '', 
PRIMARY KEY (`adminId`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; 

-- 
-- Dumping data for table `community_admin` 
-- 

INSERT INTO `community_admin` VALUES (1, 'admin', 'admin', 'Administrator', '2006-02-09', 'y', 'u'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `community_pages` 
-- 

CREATE TABLE `community_pages` ( 
`home` text NOT NULL, 
`about` text NOT NULL, 
`contact` text NOT NULL, 
`webmasters` text NOT NULL, 
`bookmark` text NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

-- 
-- Dumping data for table `community_pages` 
-- 

INSERT INTO `community_pages` VALUES ('<P>this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test </P>\r\n<P>&nbsp;</P>\r\n<P>home page</P>', '<P>this is test </P>\r\n<P>about usccc</P>', '<P>this is test contact us</P>\r\n<P>&nbsp;</P>\r\n<P>&nbsp;</P>', '<P>this is test </P>\r\n<P>webmasters</P>', '<P>this is test </P>\r\n<P>bookmark</P>'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `community_settings` 
-- 

CREATE TABLE `community_settings` ( 
`site_name` varchar(200) NOT NULL default '', 
`site_url` varchar(200) NOT NULL default '', 
`admin_email` varchar(200) NOT NULL default '', 
`results` varchar(100) NOT NULL default '' 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

-- 
-- Dumping data for table `community_settings` 
-- 

INSERT INTO `community_settings` VALUES ('Community Website', 'http://demos.esolz.net/community/programming/', 'admin@community.com', '10'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `community_user` 
-- 

CREATE TABLE `community_user` ( 
`userId` int(15) NOT NULL auto_increment, 
`firstname` varchar(200) NOT NULL default '', 
`lastname` varchar(200) NOT NULL default '', 
`email` varchar(200) NOT NULL default '', 
`password` varchar(50) NOT NULL default '', 
`dateofbirth` varchar(200) NOT NULL default '', 
`gender` char(1) NOT NULL default '', 
`profession` varchar(200) NOT NULL default '', 
`country` varchar(200) NOT NULL default '', 
`postcode` int(10) NOT NULL default '0', 
`city` varchar(200) NOT NULL default '', 
`nickname` varchar(200) NOT NULL default '', 
`profileheading` varchar(255) NOT NULL default '', 
`aboutyourself` text NOT NULL, 
`profilelink` varchar(255) NOT NULL default '', 
`bloglink` varchar(255) NOT NULL default '', 
`link1` varchar(255) NOT NULL default '', 
`link1pass` varchar(50) NOT NULL default '', 
`link2` varchar(255) NOT NULL default '', 
`link2pass` varchar(50) NOT NULL default '', 
`link3` varchar(255) NOT NULL default '', 
`link3pass` varchar(50) NOT NULL default '', 
`link4` varchar(255) NOT NULL default '', 
`link4pass` varchar(50) NOT NULL default '', 
`status` char(1) NOT NULL default '', 
PRIMARY KEY (`userId`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; 

-- 
-- Dumping data for table `community_user` 
-- 

INSERT INTO `community_user` VALUES (1, 'Atanu', 'Nandy', 'esolz_a.nandy@yahoo.co.in', '123456', '13_2_1980', 'M', 'S', '30', 100000, 'kolkata', 'Atanu_Nandy', 'Heading', 'Funny', 'http://atanu.com', 'http://atanu.com', '', '', '', '', '', '', '', '', 'u');
Now i need a php script to execute the sql file to create the tables and insert datas.

Please give the related codes and suggestion.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by basudeb on Wed Dec 20, 2006 2:03 am, edited 3 times in total.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Run a query containing the sql?
mysql_connect(), mysql_select_db(), mysql_query() might be of use.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

Try to do with database and if you have problem, so post on forum.

:)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"Give you the codes" -- nice.

Look through raghavan20's posts. There's a distinct possibility that the code you are asking for exists in part or in whole somewhere on this forum, if only we had a search tool.

:roll:
Post Reply