Salman Aslam
Salman Aslam

Reputation: 781

How to execute mysql script file in PHP?

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.

Upvotes: 4

Views: 4073

Answers (2)

John Watson
John Watson

Reputation: 2573

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.

Upvotes: 2

genesis
genesis

Reputation: 50966

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

Upvotes: 4

Related Questions