Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Name of the file
- $filename = 'my_file.sql';
- // MySQL host
- $mysql_host = 'localhost';
- // MySQL username
- $mysql_username = 'mysql_username';
- // MySQL password
- $mysql_password = 'the_password';
- // Database name
- $mysql_database = 'mysql_db_name';
- $conn =new mysqli($mysql_host,$mysql_username,$mysql_password,$mysql_database);
- $query = '';
- $sqlScript = file('my_file.sql');
- foreach ($sqlScript as $line) {
- $startWith = substr(trim($line), 0 ,2);
- $endWith = substr(trim($line), -1 ,1);
- if (empty($line) || $startWith == '--' || $startWith == '/*' || $startWith == '//') {
- continue;
- }
- $query = $query . $line;
- if ($endWith == ';') {
- mysqli_query($conn,$query) or die('<div class="error-response sql-import-response">Problem in executing the SQL query <b>' . $query. '</b></div>');
- $query= '';
- }
- }
- echo '<div class="success-response sql-import-response">SQL file imported successfully</div>';
- ?>
Add Comment
Please, Sign In to add comment