how can I import a .sql file with PHP

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Leb_CRX
Forum Newbie
Posts: 9
Joined: Thu May 18, 2006 8:27 am

how can I import a .sql file with PHP

Post by Leb_CRX »

I can't seem to find this, I've tried searching and so far, nothing

I have a MySQL dump file being transferred between servers, on the new server I want to run a script that imports it into a MySQL...it can't be done using a prompt, since permissions don't allow it

is there anything built into PHP that can do this? I've tried writing a few scripts and keep coming close, but nothing seems to be working
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how can I import a .sql file with PHP

Post by Christopher »

Is it an SQL or CSV file? If it is SQL then open, read it and run the queries. If it is CSV then use "LOAD DATA INFILE".
(#10850)
Leb_CRX
Forum Newbie
Posts: 9
Joined: Thu May 18, 2006 8:27 am

Re: how can I import a .sql file with PHP

Post by Leb_CRX »

arborint wrote:Is it an SQL or CSV file? If it is SQL then open, read it and run the queries. If it is CSV then use "LOAD DATA INFILE".
it's an SQL

I finally found a script, and it is:

Code: Select all

        $sql = explode(';', file_get_contents('healthm.sql'));
        foreach ($sql as $key => $val) {
            mysql_query($val);
        }
I overcomplicated it...it was my bad
Post Reply