嘗試使用 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中文網其他相關文章!