i want to import my backup database(.sql file) into mysql using php script. i have given the following code where case 1 section works. but i need to execute case 0 section. what the problem in my code please help me anyone.

<?php
//ENTER THE RELEVANT INFO BELOW
$mysqlDatabaseName ='online_admission_form';
$mysqlUserName ='root';
$mysqlPassword ='';
$mysqlHostName ='localhost';
$mysqlImportFilename ='db-backup-1360387884-770ac5920c7155e73215540b30ed1c18.sql';

//DONT EDIT BELOW THIS LINE
//Export the database and output the status to the page
$command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename;
exec($command,$output=array(),$worked);
switch($worked){
    case 0:
        echo 'Import file <b>' .$mysqlImportFilename .'</b> successfully imported to database <b>' .$mysqlDatabaseName .'</b>';
        break;
    case 1:
        echo 'There was an error during import. Please make sure the import file is saved in the same folder as this script and check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr><tr><td>MySQL Import Filename:</td><td><b>' .$mysqlImportFilename .'</b></td></tr></table>';
        break;
}
?>  

Dani AI

Generated

case 0 is the success path, as noted. The reason you are hitting the error branch is usually one (or more) of these: using -p with a space before the password (the mysql client treats -p without an attached value as "prompt for a password", which fails in non‑interactive exec), not quoting arguments, missing mysql in the PATH, or exec() being disabled by PHP. The mysql client expects --password=secret (or -psecret) with no space; if the password is empty, use --skip-password. Putting passwords on the command line is discouraged; consider an option file instead. (dev.mysql.com)

Try a safer, more verbose command build and capture stderr so you see the real error (use the absolute path to mysql if possible):

$mysql = '/usr/bin/mysql';  // adjust for your server
$args = [
  escapeshellarg($mysql),
  '--host=' . escapeshellarg($mysqlHostName),
  '--user=' . escapeshellarg($mysqlUserName),
  $mysqlPassword === '' ? '--skip-password'
                        : '--password=' . escapeshellarg($mysqlPassword),
  escapeshellarg($mysqlDatabaseName),
];
$cmd = implode(' ', $args) . ' < ' . escapeshellarg($mysqlImportFilename) . ' 2>&1';
$output = [];
$status = 1;
exec($cmd, $output, $status);
echo $status === 0 ? 'Import OK' : "Import failed:\n" . implode("\n", $output);
  • If you still get the generic error mentioned, confirm PHP can execute shell commands: many hosts disable exec() via disable_functions in php.ini. Check and adjust that setting (or ask your host). (php.net)
  • Also verify the mysql binary path and that the .sql file is readable by the web server user. The 2>&1 redirection will surface permission and syntax issues. (php.net)

Re @diafol’s non‑CLI idea: it can work for simple dumps, but many backups include DELIMITER and stored routines. DELIMITER is a mysql client command, not SQL, so feeding such dumps through PDO/mysqli directly will choke. In those cases, the mysql client route above is the reliable choice. (dev.mysql.com)

Tip: if your dump contains non‑ASCII data, ensure the right character set during restore (for example via client settings or option files) to avoid mojibake. (dev.mysql.com)

Recommended Answers

All 3 Replies

What would you like to do when case 0? I think case 0 is when the import went without any problems. Then you don't have to do anything else apart from letting users know (which has already been done).

This code is not work and gives and error:

There was an error during import. Please make sure the import file is saved in the same folder as this script and check your values:

Member Avatar for Member #120589

You may find this helpful: http://stackoverflow.com/a/19752106

Your code is similar to the OP, but there is a non-cli option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.