title: Complicated SQL function, 'Syntax error or access violation: 1065 Query was empty'
body: <p>i have a function <code>apphp_db_install($sql_dump_file)</code> that installs sql database from db_dump.sql file, i get a bad error:</p>
  
  <blockquote>
    <p>SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty</p>
  </blockquote>
  
  <p>I already tried to use this function in a clean database.</p>
  
  <p>My function to install SQL: </p>
  
  <pre><code>function apphp_db_install($sql_dump_file) {
      global $error_mg;
      global $username;
      global $password;
      global $database_prefix;
      global $password_encryption;
      global $db;
  
      $sql_array = array();
      $query = "";
  
  // get  sql dump content
      $sql_dump = file($sql_dump_file);
  
  // replace database prefix if exists
      $sql_dump = str_ireplace('&lt;DB_PREFIX&gt;', $database_prefix, $sql_dump);
  
  // disabling magic quotes at runtime
      if(get_magic_quotes_runtime()){
          function stripslashes_runtime(&amp;$value){
              $value = stripslashes($value);  
          }
          array_walk_recursive($sql_dump, 'stripslashes_runtime');
      }
  
  // add ";" at the end of file
      if(substr($sql_dump[count($sql_dump)-1], -1) != ";") { $sql_dump[count($sql_dump)-1] .= ";"; }      
  
  // replace username and password if exists
      if(EI_USE_USERNAME_AND_PASWORD){
          $sql_dump = str_ireplace("&lt;USER_NAME&gt;", $username, $sql_dump);
          if(EI_USE_PASSWORD_ENCRYPTION){
              if($password_encryption == "AES"){
                  $sql_dump = str_ireplace("&lt;PASSWORD&gt;", "AES_ENCRYPT('".$password."', '".EI_PASSWORD_ENCRYPTION_KEY."')", $sql_dump);
              }else if($password_encryption == "MD5"){
                  $sql_dump = str_ireplace("&lt;PASSWORD&gt;", "MD5('".$password."')", $sql_dump);
              }else{
                  $sql_dump = str_ireplace("&lt;PASSWORD&gt;", "AES_ENCRYPT('".$password."', '".EI_PASSWORD_ENCRYPTION_KEY."')", $sql_dump);                
              }
          }else{
              $sql_dump = str_ireplace("&lt;PASSWORD&gt;", "'".$password."'", $sql_dump);
          }
      }else{
          $sql_dump = str_ireplace("&lt;USER_NAME&gt;", "", $sql_dump);
          $sql_dump = str_ireplace("&lt;PASSWORD&gt;", "''", $sql_dump);
      }
      $sql_dump = str_ireplace("&lt;ENCRYPTION_TYPE&gt;", $password_encryption, $sql_dump);
  
  // encode connection, server, client etc.   
      if(EI_USE_ENCODING){
          $db-&gt;SetEncoding(EI_DUMP_FILE_ENCODING, EI_DUMP_FILE_COLLATION);
      }       
  
      foreach($sql_dump as $sql_line){
          $tsl = trim(utf8_decode($sql_line));
          if(($sql_line != "") &amp;&amp; (substr($tsl, 0, 2) != "--") &amp;&amp; (substr($tsl, 0, 1) != "?") &amp;&amp; (substr($tsl, 0, 1) != "#")) {
              $query .= $sql_line;
              if(preg_match("/;\s*$/", $sql_line)){
                  if(EI_MODE == "debug"){
                      if(!$db-&gt;Query($query)){ $error_mg[] = $db-&gt;Error(); return false; }                        
                  }else{
                      if(!@$db-&gt;Query($query)){ $error_mg[] = $db-&gt;Error(); return false; }
                  }
                  $query = "";
              }
          }
      }
      return true;
  }
  </code></pre>
  
  <p>And to make connection:</p>
  
  <pre><code>$db = new Database($database_host, $database_name, $database_username, $database_password, $database_type, false, true);
  if($db-&gt;Open())
  {
      $sql_dump_result = file_get_contents($sql_dump);
      if($sql_dump_result != "")
      {   
      if(false == ($db_error = apphp_db_install($sql_dump)))
                    {
         $error_mg[] = "SQL execution error! Please check carefully a syntax of SQL dump file.";                      
                      } else { echo 'success'; }
      } 
  }
  </code></pre>
  
  <p>I searched on internet <strong>days</strong> and i didn't found something that can help me...</p>