4

I have almost 100MB of example.sql file. (Actually data export file from another database)

I want to give user the ability to run this file through some interface in my application. Can you please guide me how can i do that? Should i treat it as simple text file? Either there is any quick way???

In other words, I want to add same functionality as we have in phpMyAdmin, The import functionality.

If you can refer me some class on PHPclass.org that will be great.

1
  • let user upload that file and push it in mysqldump or something like that Commented Oct 20, 2011 at 17:55

2 Answers 2

4
function import_file($filename){
    if ($file = file_get_contents($filename)){
        foreach(explode(";", $file) as $query){
            $query = trim($query);
            if (!empty($query) && $query != ";") {
                mysql_query($query);
            }
        }
    }
}

can be called with

import_file("files/files.sql");

However, this function will not work properly if file includes semicolon ; somewhere else than at the end of the query

Sign up to request clarification or add additional context in comments.

11 Comments

I am just trying, seems like best option. Will be back to select it as accepted answer :)
This'll blow up hideously if any of the actual data contains a ;. But for quick 'n dirty, it's "good enough"
@MarcB: Wow, didn't even think about this situation. In that case there would be a lot of things to be considered.
@Herbert: php's mysql driver disables multiple queries in a single call as a security measure against sql injection attacks.
@Rocket: It's fortunately when speaking about SQLi. This can't happen with mysql_query()
|
2

Create a file upload form that allows trusted users to upload the file to your server. Then call the mysql command-line tools from PHP to import the data into your database. Doing it by trying to explode(";", ...) will fail if there are any quoted semicolons within the imported data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.