2009
08.04
08.04
Few days ago i get 15MB .sql file in which only one table was there with data. My task is to just import those data in database. Now i have phpMyAdmin but it can allow me 8MB of file. Even i do not have any other support on server. So now i have to write my code or find some code on internet. I tried but dont get any proper solution. so i decided to write my own code. I have used this code to import full databases also for testing.
Here is code
<?php
// file name to import
$sqlfile = 'myfile.sql';
$mysql_host = 'localhost';
$mysql_username = 'root';
$mysql_password = '';
$mysql_database = 'database_name';
mysql_connect($mysql_host, $mysql_username, $mysql_password);
mysql_select_db($mysql_database);
// define Temporary variable.
$temp = '';
// read file
$lines = file($sqlfile);
// get line by line loop from file
foreach ($lines as $line)
{
// we have to omit comment.
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// if it not comment than save it in $temp variable
$temp. = $line;
// If we get a semicolon at the end of line than
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
mysql_query($temp);
// set temp variable to empty
$temp = '';
}
}
?>