尝试使用 PHP 脚本导入 .sql 文件时,您可能会遇到错误。要解决此问题,请验证 SQL 文件是否与脚本位于同一目录中。
以下是应该有效的修改后的代码:
<code class="php"><?php // Enter the database info $mysqlDatabaseName = 'test'; $mysqlUserName = 'root'; $mysqlPassword = ''; $mysqlHostName = 'localhost'; $mysqlImportFilename = 'dbbackupmember.sql'; // Execute the MySQL import command $command = 'mysql -h' . $mysqlHostName . ' -u' . $mysqlUserName . ' -p' . $mysqlPassword . ' ' . $mysqlDatabaseName . ' < ' . $mysqlImportFilename; exec($command, $output, $worked); // Display a message based on the import status 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 border="1"> <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; } ?></code>
避免使用过时的 mysql_* 函数。相反,请考虑使用 mysqli 或 PDO_MySQL 扩展。
以上是使用 PHP 将 .sql 文件导入 MySQL 数据库时如何解决错误?的详细内容。更多信息请关注PHP中文网其他相关文章!