php program to read .sql file and auto generate table and insert data in MySQL database? (i export all the database table..sql i want a php program which read the .sql file and auto generate all the table and insert data into it..)
thanks in advance..
try
<?php
if(isset($_FILE["file"])){
$dshost = "";
$dsdatabase = "";
$dsusername = "";
$dspassword = "";
$connection1 = mysql_connect($dshost,$dsusername,$dspassword);
$seldb = mysql_select_db($dsdatabase, $connection1) or die( "Unable to select database");
$sql = file_get_contents($_FILE['file']);
$q=mysql_query($sql) or die(mysql_error());
if($q){
echo "Success!";
}else{
echo "Fail.";
}
}else{
?>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
?>
I think I did that right. Except people will yell at me if I don't tell you to use PDO or mysqli instead. obviously you need to add your own sql creds at the top.
ok here it is, if you know even little bit about programming you'll understand else you need to goto school. this is command and data to make tables
CREATE TABLE IF NOT EXISTS `wp_options` (
`option_id` bigint(20) unsigned NOT NULL auto_increment,
`option_name` varchar(64) NOT NULL default '',
`option_value` longtext NOT NULL,
`autoload` varchar(20) NOT NULL default 'yes',
PRIMARY KEY (`option_id`),
UNIQUE KEY `option_name` (`option_name`)
) ENGINE=MyISAM AUTO_INCREMENT=151 DEFAULT CHARSET=utf8;
and below is data, please note that it is just example, we are not gonna do exactly what you want to do but we can help you get started.
INSERT INTO `wp_options` VALUES (1, 'siteurl', 'http://www.lostknowledge.org', 'yes');
INSERT INTO `wp_options` VALUES (2, 'blogname', 'Lost Knowledge', 'yes');
INSERT INTO `wp_options` VALUES (3, 'blogdescription', 'Just another WordPress site', 'yes');
INSERT INTO `wp_options` VALUES (4, 'users_can_register', '0', 'yes');
INSERT INTO `wp_options` VALUES (5, 'admin_email', '8156345@gmail.com', 'yes');
INSERT INTO `wp_options` VALUES (6, 'start_of_week', '1', 'yes');
INSERT INTO `wp_options` VALUES (7, 'use_balanceTags', '0', 'yes');
INSERT INTO `wp_options` VALUES (8, 'use_smilies', '1', 'yes');
INSERT INTO `wp_options` VALUES (9, 'require_name_email', '1', 'yes');
INSERT INTO `wp_options` VALUES (10, 'comments_notify', '1', 'yes');
INSERT INTO `wp_options` VALUES (11, 'posts_per_rss', '10', 'yes');
INSERT INTO `wp_options` VALUES (12, 'rss_use_excerpt', '0', 'yes');
INSERT INTO `wp_options` VALUES (13, 'mailserver_url', 'mail.example.com', 'yes');
given the question that you want to use it from file you can use &sql = file_get_contents(filename.sql) and harki krishnan told and then insert $sql after command create table if not exists
Try below library, it will help you to upload .sql file and execute it's content