[英]How to use mysql dump in php
I've tried using mysql dump in the command line and it worked. 我试过在命令行中使用mysql dump,它起作用了。 How do I do it in php?
我如何在php中做到这一点? I found this code from the internet, and tried it.
我从互联网上找到了此代码,并对其进行了尝试。
<?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";
I don't really understand what this following code means: system($command); 我不太明白以下代码的含义:system($ command);
$dump = ob_get_contents();
ob_end_clean();
$fp = fopen("dump.sql", "w");
fputs($fp, $dump);
fclose($fp);
?>
I imported the generated .sql file to a database and I got this error from phpmyadmin: 我将生成的.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
Please help, I want to learn how to do this in php. 请帮助,我想学习如何在php中做到这一点。
system($command);
executes the external mysqldump
command. 执行外部
mysqldump
命令。
I don't really understand the script you pasted, it looks like a mixture of two scripts? 我不太了解您粘贴的脚本,它看起来像是两个脚本的混合?
Anyway, the syntax errors in the dump file probably mean that you have a version mismatch (4.x running on the target server?). 无论如何,转储文件中的语法错误可能意味着您的版本不匹配(目标服务器上正在运行4.x吗?)。
Use the compatible
switch to generate dump files that work in the target version, .eg 使用
compatible
开关生成适用于目标版本(例如.eg)的转储文件
--compatible=mysql40
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.