0

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.