简体   繁体   中英

Complicated SQL function, 'Syntax error or access violation: 1065 Query was empty'

i have a function apphp_db_install($sql_dump_file) that installs sql database from db_dump.sql file, i get a bad error:

SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty

I already tried to use this function in a clean database.

My function to install SQL:

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('<DB_PREFIX>', $database_prefix, $sql_dump);

// disabling magic quotes at runtime
    if(get_magic_quotes_runtime()){
        function stripslashes_runtime(&$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("<USER_NAME>", $username, $sql_dump);
        if(EI_USE_PASSWORD_ENCRYPTION){
            if($password_encryption == "AES"){
                $sql_dump = str_ireplace("<PASSWORD>", "AES_ENCRYPT('".$password."', '".EI_PASSWORD_ENCRYPTION_KEY."')", $sql_dump);
            }else if($password_encryption == "MD5"){
                $sql_dump = str_ireplace("<PASSWORD>", "MD5('".$password."')", $sql_dump);
            }else{
                $sql_dump = str_ireplace("<PASSWORD>", "AES_ENCRYPT('".$password."', '".EI_PASSWORD_ENCRYPTION_KEY."')", $sql_dump);                
            }
        }else{
            $sql_dump = str_ireplace("<PASSWORD>", "'".$password."'", $sql_dump);
        }
    }else{
        $sql_dump = str_ireplace("<USER_NAME>", "", $sql_dump);
        $sql_dump = str_ireplace("<PASSWORD>", "''", $sql_dump);
    }
    $sql_dump = str_ireplace("<ENCRYPTION_TYPE>", $password_encryption, $sql_dump);

// encode connection, server, client etc.   
    if(EI_USE_ENCODING){
        $db->SetEncoding(EI_DUMP_FILE_ENCODING, EI_DUMP_FILE_COLLATION);
    }       

    foreach($sql_dump as $sql_line){
        $tsl = trim(utf8_decode($sql_line));
        if(($sql_line != "") && (substr($tsl, 0, 2) != "--") && (substr($tsl, 0, 1) != "?") && (substr($tsl, 0, 1) != "#")) {
            $query .= $sql_line;
            if(preg_match("/;\s*$/", $sql_line)){
                if(EI_MODE == "debug"){
                    if(!$db->Query($query)){ $error_mg[] = $db->Error(); return false; }                        
                }else{
                    if(!@$db->Query($query)){ $error_mg[] = $db->Error(); return false; }
                }
                $query = "";
            }
        }
    }
    return true;
}

And to make connection:

$db = new Database($database_host, $database_name, $database_username, $database_password, $database_type, false, true);
if($db->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'; }
    } 
}

I searched on internet days and i didn't found something that can help me...

It seems like there is empty lines in your SQL dump, that you try to execute (so the query for this line is indeed empty)

First fix, the test $sql_line != "" is useless since file() function returns the end-of-line character at the end of each line. You can replace the test per trim($sql_line) != ""

If it is not enough, you can add a test (trim($sql_line) != ';') to remove lines that only contain a ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM