0

I can't use mysqldump without getting an error.

I'm writing a script in php that is supposed to make a backup of my data, however I can't get mysqldump to work as a command.

I'm only having trouble with the mysqldump command in the exec() method of PHP.

exec("mysqldump --user=exampleUser --host=exampleHost exampleDB > filepath/file.sql");

The expected result is that a new file called file.sql gets created under the given filepath (creating the file works) and that in this file all information is saved. The actual result is an empty file.sql under the given path. The Error message is:

The command "mysqldump" is either misspelled or could not be found.

Sfili_81
  • 2,377
  • 8
  • 27
  • 36
Lovahrk
  • 23
  • 7

1 Answers1

0

You could try something like this

<?php

    /* For a unix like system, use this */
    exec('$(which mysqldump) -u admin -p test', $output);

    /* For windows use this, after adding to `mysql bin` folder to your PATH */
    exec('mysqldump -u admin -p test', $output);

    $content = implode("\n", $output);
    $path = '';
    $file = $path . 'test.sql';
    file_put_contents($file, $content);

Note: You need to add mysql bin folder to your path for windows, if that is what you are using and you can follow instructions in this link how to do it if used the official standalone mysql installation and if you used xampp you can go to this link on how to do it

kellymandem
  • 1,709
  • 3
  • 17
  • 27