Guest User

Untitled

a guest
Jan 28th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. <?php
  2.  
  3. // Name of the file
  4. $filename = 'my_file.sql';
  5. // MySQL host
  6. $mysql_host = 'localhost';
  7. // MySQL username
  8. $mysql_username = 'mysql_username';
  9. // MySQL password
  10. $mysql_password = 'the_password';
  11. // Database name
  12. $mysql_database = 'mysql_db_name';
  13.  
  14. $conn =new mysqli($mysql_host,$mysql_username,$mysql_password,$mysql_database);
  15.  
  16. $query = '';
  17. $sqlScript = file('my_file.sql');
  18. foreach ($sqlScript as $line) {
  19.  
  20. $startWith = substr(trim($line), 0 ,2);
  21. $endWith = substr(trim($line), -1 ,1);
  22.  
  23. if (empty($line) || $startWith == '--' || $startWith == '/*' || $startWith == '//') {
  24. continue;
  25. }
  26.  
  27. $query = $query . $line;
  28. if ($endWith == ';') {
  29. mysqli_query($conn,$query) or die('<div class="error-response sql-import-response">Problem in executing the SQL query <b>' . $query. '</b></div>');
  30. $query= '';
  31. }
  32. }
  33. echo '<div class="success-response sql-import-response">SQL file imported successfully</div>';
  34. ?>
Add Comment
Please, Sign In to add comment