Export mysql table into SQL format?
Solution 1
Do not reinvent the wheel. What you need already exists almost out-of-the-box:
<?php
$result = exec("/path/to/mysqldump -u$username -p$password your_database your_table > /desired/output/path/dump.sql");
You may want to check the contents of $result
afterwads, to make sure everything went smooth.
Solution 2
You are getting the error because you are using a deprecated function ereg_replace
This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.
I would suggest you to use preg_replace()
as alternative to this function.
Ali Hamra
Updated on June 04, 2022Comments
-
Ali Hamra about 2 years
Is it possible to export a table with its records into SQL format via PHP code ? I've been looking around and all I found is exporting it as CSV file.
However I found the code below :
backup_tables('localhost','root','','compare_db'); function backup_tables($host,$user,$pass,$name,$tables = '*') { $link = mysql_connect($host,$user,$pass); mysql_select_db($name,$link); //get all of the tables if($tables == '*') { $tables = array(); $result = mysql_query('SHOW TABLES'); while($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } } else { $tables = is_array($tables) ? $tables : explode(',',$tables); } //cycle through foreach($tables as $table) { $result = mysql_query('SELECT * FROM '.$table); $num_fields = mysql_num_fields($result); $return.= 'DROP TABLE '.$table.';'; $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $return.= "\n\n".$row2[1].";\n\n"; for ($i = 0; $i < $num_fields; $i++) { while($row = mysql_fetch_row($result)) { $return.= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j<$num_fields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = ereg_replace("\n","\\n",$row[$j]); if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; } if ($j<($num_fields-1)) { $return.= ','; } } $return.= ");\n"; } } $return.="\n\n\n"; } //save file $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+'); fwrite($handle,$return); fclose($handle); }
It outputs the following error :
Notice: Undefined variable: return
and
Deprecated: Function ereg_replace() is deprecated
All I want is to export a SINGLE table via PHP into .sql format !....I'm not that familiar with PHP, but hope you can help me out !
-
RandomSeed about 11 yearsDon't reinvent the wheel.
mysqldump
already exists. You may want to issue a system call to it. -
user428517 about 11 yearslook into
mysqldump
... would probably be easier for you to use and you can always run it from php viaexec
orsystem
. -
Ali Hamra about 11 yearsI did not get it, could you explain briefly please !
-
-
Ali Hamra about 11 yearsI've tried your code as follows :
exec('C:/wamp/bin/mysql/mysql5.5.24/bin/mysqldump compare_db collected_items > compare/dump.sql');
But it did not work :( -
RandomSeed about 11 yearsWhat didn't work? What does
$result
contain? Try with an absolute path (C:/fullpath/to/compare/dump.sql
). Does this directory exist? Does PHP have write access to it? -
Ali Hamra about 11 yearsThe absolute path was the issue ! Thank you so much, now its working as needed :) !