在php中导出.sql

时间:2013-04-15 10:12:46

标签: php sql phpmyadmin

我想从daatabase使用php导出sql和excel文件。尝试了在这里找到的所有薄片,但没有工作。

查看代码显示错误"严格标准:只应在C:\ xampp \ htdocs \ dan \ index.php中通过引用传递变量 在学校出口到〜/ one.sql"

时有一个警告
<?php

$mysqlDatabaseName ='school';
$mysqlUserName ='root';
$mysqlPassword ='';
$mysqlHostName ='';
$mysqlExportPath ='one.sql';


$command='mysqldump --opt -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' > ~/' .$mysqlExportPath;
exec($command,$output=array(),$worked);
switch($worked){
    case 0:
        echo 'Database <b>' .$mysqlDatabaseName .'</b> successfully exported to <b>~/' .$mysqlExportPath .'</b>';
        break;
    case 1:
        echo 'There was a warning during the export of <b>' .$mysqlDatabaseName .'</b> to <b>~/' .$mysqlExportPath .'</b>';
        break;
    case 2:
        echo 'There was an error during export. Please 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></table>';
        break;
}
?>

1 个答案:

答案 0 :(得分:0)

  1. 指定mysqldump
  2. 的完整路径
  3. 如果主机名和密码为空,请不要指定相应的选项
  4. 确保导出路径可写
  5. 试试这个

    $output = array();
    $worked = 2;
    $command = "/usr/local/mysql/bin/mysqldump --opt";
    if (!empty($mysqlHostName))
        $command .= " -h$mysqlHostName";
    $command .= " -u$mysqlUserName ";
    if (!empty($mysqlPassword)) 
        $command .= " -p'$mysqlPassword'";
    $command .= " $mysqlDatabaseName > /tmp/$mysqlExportPath";
    // your code goes here
    

    调整mysqldump的路径并相应地导出文件夹