1

I try to dump my data thought php script but without command line. SO I create my .sql file with this script and then I try with my script:

$link = mysql_connect($host, $user, $pass);
mysql_select_db($name, $link);
$sqlFile = 'sql.sql';
$fsize = filesize($sqlFile);
$fopen = fopen($sqlFile, 'r');
$fread = fread($fopen, $fsize);
fclose($fopen);
mysql_query($fread) or die(mysql_error());
mysql_close();

When I try to dump data I got a error:

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 'CREATE TABLE `ps_access` (`id_profile` int(10) unsigned NOT NULL,`id_tab` int(10' at line 1

But when I paste the sql in phpMyAdmin sql input tab theres no problem with this sql code:

DROP TABLE IF EXISTS `ps_access`;
CREATE TABLE `ps_access` (
`id_profile` int(10) unsigned NOT NULL,
`id_tab` int(10) unsigned NOT NULL,
`view` int(11) NOT NULL,
`add` int(11) NOT NULL,
`edit` int(11) NOT NULL,
`delete` int(11) NOT NULL,
PRIMARY KEY (`id_profile`,`id_tab`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

So where is my error in php script and how to fix it to dump the data?

Avarage SQL file is ~800KB

Best regards, George!

2
  • Does it work if you remove the first line DROP TABLE IF EXISTS... Commented Jun 18, 2012 at 1:04
  • But the script MUST working with it :) Commented Jun 18, 2012 at 1:24

2 Answers 2

1

The error happens because mysql_query doesn't support multiple queries:

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

(source)

Theoretically you could use explode + a foreach loop to send each query separately, but in practice this may not work as expected. See: Loading .sql files from within PHP

Sign up to request clarification or add additional context in comments.

3 Comments

So If I believe in your words - There is no way to dump the data thought php or ...?
You can if you use a driver that supports multiple queries at once, such as PDO or mysqli (as opposed to the deprecated mysql_* functions) E.g: stackoverflow.com/a/7178917/1329367
Hi, I do that with mysqli_multi_query() but the function may have some limit of length and I explode the file to few parts and then everything works fine :) Thank you!
1

If your average sql file is about 800Kb and what you have shown, is part of it, you are having multiple sql statements in your string (apart from the two you have shown...) and you can only run a single sql query through the (deprecated...) mysql_query function.

1 Comment

Yes, its a part of all because I think You know how much is 800kb sql :) So any ideas how to dump the data? Regards!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.