Home >Backend Development >PHP Tutorial >How to Fix \'Cannot Locate Import File\' Error When Importing a .sql File into MySQL Database Using PHP?
How to Import a .sql File in a MySQL Database Using PHP
You are encountering an error while attempting to import a .sql file into a MySQL database using PHP. The error message indicates that the import file cannot be located or that the specified values are incorrect.
To resolve this issue, you can try the following:
Alternative Method Using the mysqli Extension
<code class="php"><?php // Connect to MySQL server $mysqli = new mysqli('localhost', 'root', '', 'database_name'); // Read in SQL file $sql = file_get_contents('db_backup.sql'); // Execute SQL statements one by one $sql_statements = explode(';', $sql); foreach ($sql_statements as $statement) { if (trim($statement)) { $mysqli->query($statement); } } // Close connection $mysqli->close(); echo "Tables imported successfully"; ?></code>
Note:
The mysql_* functions used in your code are deprecated in PHP 5.5 and removed in PHP 7. It is recommended to use the mysqli or PDO extensions for database operations.
The above is the detailed content of How to Fix \'Cannot Locate Import File\' Error When Importing a .sql File into MySQL Database Using PHP?. For more information, please follow other related articles on the PHP Chinese website!