shell_exec("mysql -h[MYSQL_HOST] -u[MYSQL-username] -p[MYSQL-PASSWORD] [MYSQL_DB] < file.sql");
Main Topics
Browse All TopicsI have a .sql file which I get upon exporting a table (both structure and data) from a database using phpmyadmin. But now I want to import it into another database, but using php. So please help me with a solution for this.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: fulscherPosted on 2005-11-12 at 00:06:46ID: 15279164
This is the code I use for backup and restore. Works very nicely. Using the restore function only on a table created by PHP admin should work.
////////// ////////// ////////// ////////// ////////// //// ========== ========== ========== ========== ========== === ========== ========== ========== ========== ========== === ////////// ////////// ////////// ////////// ////////// ////
'database' ]);
ow[0])); w[0]));
ablename)
)
)
) {
blename)
$i++) { $i);
$i++) { i]));
ay, $backup_structure, $backup_data)
ablename_a rray[$i]); blename_ar ray[$i]);
cted);
<?
//////////////////////////
//
// Class name: mysql_backup
// PHP version: 4.0.2
// Function: Back up/Restore a MySql db.
// Written by: Peyman Hooshmandi Raad
// Date: Jan 9, 2003
// Minor adaptions by Jan Fuelscher, 08-2005
// Contact: captain5ive@yahoo.com
//========================
//
// Methods:
//========================
// 1) Backup this method will back up the whole database.
// $output: text file name and path(the one that holds backup)
// $structure_only: (true / false)
// true : backup method will only make backup from tables NOT the data.
// false : backup method will make backup from tables and the data(WHOLE DB).
//
// 2) Restor This method will create table with data from a txt file.
//
// Copyright: this class is free for non commercial uses.
//
//////////////////////////
DEFINE ("DEBUG", 0);
function backup($filename)
{
global $dbdata;
$result = mysql_list_tables($dbdata[
$num_tables = mysql_num_rows($result);
$tablenames = array();
$fh = fopen($filename, "w");
while ($row = mysql_fetch_row($result))
{
fputs($fh, create_table_sql_string($r
fputs($fh, "\n");
fputs($fh, create_data_sql_string($ro
}
fclose($fh);
}
function create_table_sql_string($t
{
// Start the SQL string for this table
$sql_string = "CREATE TABLE $tablename";
$field_string = '';
$index_string = '';
// Get the field info and output to a string in the correct MySQL syntax
$result = mysql_query("DESCRIBE $tablename");
if (DEBUG) echo "field_info\n\n";
while ($field_info = mysql_fetch_array($result)
{
if (DEBUG)
{
for ($i = 0; $i < count($field_info); $i++)
{
echo "$i: $field_info[$i]\n";
}
}
$field_name = $field_info[0];
$field_type = $field_info[1];
$field_not_null = ($field_info[2] == "YES") ? "" : " NOT NULL";
$field_default = ($field_info[4] == NULL) ? "" : sprintf(" default '%s'", $field_info[4]);;
$field_auto_increment = ($field_info[5] == NULL) ? "" : sprintf(" %s", $field_info[5]);
$field_string = sprintf("%s, %s %s%s%s%s", $field_string, $field_name, $field_type, $field_not_null, $field_auto_increment, $field_default);
}
// Get the index info and output to a string in the correct MySQL syntax
$result = mysql_query("SHOW INDEX FROM $tablename");
if (DEBUG) echo "\nindex_info\n\n";
while ($index_info = mysql_fetch_array($result)
{
if (DEBUG)
{
for ($i = 0; $i < count($index_info); $i++) {
echo "$i: $index_info[$i]\n";
}
}
$index_name = $index_info[2];
$index_unique = $index_info[1];
$index_field_name = $index_info[4];
$index_type = $index_info[10];
if ($index_name == "PRIMARY") $index_name = "PRIMARY KEY";
if ($index_unique == "1" && $index_type != "FULLTEXT") $index_name = sprintf("KEY %s", $index_name);
if ($index_unique == "0" && $index_name != "PRIMARY KEY") $index_name = sprintf("UNIQUE KEY %s", $index_name);
if ($index_type == "FULLTEXT") $index_name = sprintf("FULLTEXT KEY %s", $index_name);
$index_string = sprintf("%s, %s (%s)", $index_string, $index_name, $index_field_name);
}
// Get the table type and output it to a string in the correct MySQL syntax
$result = mysql_query("SHOW TABLE STATUS");
if (DEBUG) echo "\nstatus_info\n\n";
while ($status_info = mysql_fetch_array($result)
for ($i = 0; $i < count($status_info); $i++) {
if (DEBUG) echo "$i: $status_info[$i]\n";
if ($status_info[0] == $tablename) $table_type = sprintf("TYPE=%s", $status_info[1]);
}
}
// Remove the first 2 characters (", ") from the field string
$field_string = substr($field_string, 2);
// Append the index string to the field string
$field_string = sprintf("%s%s", $field_string, $index_string);
// Put the field string in parantheses
$field_string = sprintf("(%s)", $field_string);
// Finalise the MySQL create table string
$sql_string = sprintf("%s %s %s", $sql_string, $field_string, $table_type);
return $sql_string;
}
function create_data_sql_string($ta
{
// Initialise the field string
$field_string = "";
$sql_string = '';
// Get field names from MySQL and output to a string in the correct MySQL syntax
$result = mysql_query("SELECT * FROM $tablename");
for ($i = 0; $i < mysql_num_fields($result);
$meta = mysql_fetch_field($result,
$field_string = sprintf("%s, %s", $field_string, $meta->name);
}
// Remove the first 2 characters (", ") from the field string
$field_string = substr($field_string, 2);
// Put the field string in parantheses
$field_string = sprintf("(%s)", $field_string);
// Get table data from MySQL and output to a string in the correct MySQL syntax
while ($row = mysql_fetch_row($result)) {
// Initialise the data string
$data_string = "";
// Loop through the records and append data to the string after escaping
for ($i = 0; $i < mysql_num_fields($result);
$data_string = sprintf("%s, '%s'", $data_string, mysql_escape_string($row[$
}
// Remove the first 2 characters (", ") from the data string
$data_string = substr($data_string, 2);
// Put the data string in parantheses and prepend "VALUES "
$data_string = sprintf("VALUES (%s)", $data_string);
// Finalise the MySQL insert into string for this record
$sql_string = sprintf("%sINSERT INTO %s %s %s\n", $sql_string, $tablename, $field_string, $data_string);
}
return $sql_string;
}
function backup_data($tablename_arr
{
for ($i = 0; $i <count($tablename_array); $i++) {
$table_sql_string = "";
$data_sql_string = "";
if ($backup_structure) $table_sql_string = create_table_sql_string($t
if ($backup_data) $data_sql_string = create_data_sql_string($ta
if ($table_sql_string) $sql_string = sprintf("%s\n%s", $sql_string, $table_sql_string);
if ($data_sql_string) $sql_string = sprintf("%s\n%s", $sql_string, $data_sql_string);
}
return $sql_string;
}
function restore_data($filename, $restore_structure, $restore_data, $db_selected)
{
$handle = fopen($filename, 'r');
if (! $handle)
return 'error opening file "' . $filename . '"';
$msg = '';
while (!feof($handle))
{
$buffer = fgets($handle);
if ($buffer != "\n" && $buffer != "")
{
// if this line is a create table query then check if the table already exists
if (substr($buffer, 0, 12) == "CREATE TABLE")
{
if ($restore_structure)
{
$tablename = explode(" ", $buffer);
$tablename = $tablename[2];
$result = mysql_list_tables($db_sele
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
if (mysql_tablename($result, $i) == $tablename)
{
/* copy old table to a random table name. Skipped here - Jan Fuelscher
$rand = substr(md5(time()), 0, 8);
$random_tablename = sprintf("%s_bak_%s", $tablename, $rand);
$query = "RENAME TABLE $tablename TO $random_tablename";
if (! mysql_query($query))
return mysql_error().": $query";
*/
$query = "DROP TABLE $tablename";
if (! mysql_query($query))
return $msg . mysql_error().": $query";
$msg .= "Dropped table $tablename.<br />\n";
}
}
$result = mysql_query($buffer);
if (!$result)
return $msg . mysql_error()."$buffer <br />\n";
else
$msg .= "Table '$tablename' successfully recreated.<br />\n";
}
}
else
{
if ($restore_data)
{
$result = mysql_query($buffer);
if (!$result)
return $msg . mysql_error()."<br />\n";
}
}
}
}
return $msg;
}
?>