如何在 php 中使用 mysql dump

发布于 2024-09-10 17:44:05 字数 1535 浏览 0 评论 0原文

我尝试在命令行中使用 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 中执行此操作。

I've tried using mysql dump in the command line and it worked. How do I do it in 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);

$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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走就滚别墨迹 2024-09-17 17:44:05

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

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

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

使用 兼容 开关生成在目标版本(.eg)中工作的转储文件

--compatible=mysql40

system($command); executes the external mysqldump command.

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?).

Use the compatible switch to generate dump files that work in the target version, .e.g.

--compatible=mysql40
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文