PHP code to execute MySQL script

There are advanced and comfortable tools to manage your MySQL databases (i.e.  server side: phpMyAdmin, client side:  SQLyog).

But sometimes you can not use those tools and need the ability to execute some SQL script on server by your own hands, easy and fast way. What to do in such situation? Use this PHP code to execute all SQL commands from SQL script text file, one by one:

$sqlFileToExecute = 'path/to/sql/script.sql';

$hostname = DB_HOST;
$db_name = DB_NAME;
$db_user = DB_USER;
$db_password = DB_PASSWORD;

$link = mysql_connect($hostname, $db_user, $db_password);
if (!$link) {
  die ("MySQL Connection error");

mysql_select_db($db_name, $link) or die ("Wrong MySQL Database");

$query = '';
foreach ($fp as $line) {
  if ($line != '' && strpos($line, '--') === false) {
  $query .= $line;
    if (substr($query, -1) == ';') {
      $query = '';