如何使用php函数导入.sql文件

ngynwnxp  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(347)

我使用的是xampp服务器,我试图将一个表导入到我的数据库中,但该表不起作用。这是我的密码:

$mysqlDatabaseName ='postcodes';
$mysqlUserName ='root';
$mysqlPassword ='';
$mysqlHostName ='localhost';
$mysqlImportFilename ='http://localhost/import_tbl/postcode_withLatlang.sql';

$command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename;

//var_dump( file_exists('postcode_withLatlang.sql') );

$output = array();

 exec($command, $output, $worked);
print_r($output);
echo $worked;

// test whether they are imported successfully or not
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;
 }

请帮帮我

mm5n2pyu

mm5n2pyu1#

现在起作用了!首先,它在本地服务器上不工作;其次,我使用system()函数代替exec()函数,最后,它在数据库中生成一个表。谢谢大家!

4zcjmb1e

4zcjmb1e2#

你需要改变 $mysqlImportFilename ='http://localhost/import_tbl/postcode_withLatlang.sql';$mysqlImportFilename ='postcode_withLatlang.sql'; ,然后将php脚本文件放入.sql文件所在的文件夹中,并从该文件夹运行它。

相关问题