Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm doing an import function for .sql files. I tried following the steps given in this link http://ellislab.com/forums/viewthread/151225/#733423 but I keep getting the "Query was empty" error.

This is my code:

// I'm moving the uploaded file to a folder named 'path' and try to load it there.

$file = $_FILES['file']['name'];

$file_path = './path/';

move_uploaded_file($_FILES['file']['tmp_name'], $file_path.$file);

$file = $this->load->file($file_path.$file, true);

$file_array = explode(';', $file);

foreach($file_array as $query) {

$this->db->query("SET FOREIGN_KEY_CHECKS = 0");
$this->db->query($query);
$this->db->query("SET FOREIGN_KEY_CHECKS = 1");

}

After executing the function, I get the error but the data were successfully inserted to the database. I can't seem to find what's wrong here. Please help me.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Last value of $file_array maybe empty, so you should do like this:

foreach($file_array as $query) {
    if (!empty(trim($query))){
        $this->db->query("SET FOREIGN_KEY_CHECKS = 0");
        $this->db->query($query);
        $this->db->query("SET FOREIGN_KEY_CHECKS = 1");
    }
}
share|improve this answer
    
Thank you. Tried it but still no luck. –  user3079254 Dec 8 '13 at 7:15
    
change a little, have a try. –  iodragon Dec 8 '13 at 7:36
    
The line if (!empty(trim($query))) gave me a bit of error so I changed it to: $str = trim($query) if (!empty($str)) { } And now it works! I can't believe it is just this simple. Thank you very much! –  user3079254 Dec 8 '13 at 7:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.