如何在php中使用mysql dump

时间:2010-07-20 08:27:44

标签: php backup mysqldump

我尝试在命令行中使用mysql dump并且它有效。我怎么在PHP中做到这一点? 我从互联网上找到了这个代码,并尝试了它。

<?php
ob_start();

$username = "root"; 
$password = "mypassword"; 
$hostname = "localhost"; 
$sConnString = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");


$connection = mysql_select_db("test",$sConnString) 
or die("Could not select DB");
$command ="C:\wamp\bin\mysql\mysql5.1.36\bin --add-drop-table --host=$hostname --databases test > C:\wamp\www\test\tester.sql"; 

我真的不明白以下代码的含义:     系统($命令);

$dump = ob_get_contents(); 
ob_end_clean();



$fp = fopen("dump.sql", "w"); 
fputs($fp, $dump); 
fclose($fp);

?>

我将生成的.sql文件导入数据库,我从phpmyadmin收到此错误:

There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem

ERROR: Unknown Punctuation String @ 3
STR: :\
SQL: 
E:\Users\Nrew\Documents>set path=C:\wamp\bin\mysql\mysql5.1.36\bin;
E:\Users\Nrew\Documents>set path=C:\wamp\bin\mysql\mysql5.1.36\bin;
E:\Users\Nrew\Documents>set path=C:\wamp\bin\mysql\mysql5.1.36\bin;

SQL query:

E:\Users\Nrew\Documents>set path=C:\wamp\bin\mysql\mysql5.1.36\bin;

MySQL said: 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'E:\Users\Nrew\Documents>set path=C:\wamp\bin\mysql\mysql5.1.36\bin' at line 1 

请帮助,我想学习如何在php中执行此操作。

1 个答案:

答案 0 :(得分:1)

system($command);执行外部mysqldump命令。

我真的不明白你粘贴的脚本,它看起来像两个脚本的混合?

无论如何,转储文件中的语法错误可能意味着您的版本不匹配(4.x在目标服务器上运行?)。

使用compatible开关生成在目标版本中工作的转储文件,例如。

--compatible=mysql40