如何在 php 中使用 mysql dump
我尝试在命令行中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
system($command);
执行外部mysqldump
命令。我不太明白你粘贴的脚本,它看起来像是两个脚本的混合体?
无论如何,转储文件中的语法错误可能意味着版本不匹配(目标服务器上运行的是 4.x?)。
使用
兼容
开关生成在目标版本(.eg)中工作的转储文件system($command);
executes the externalmysqldump
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.