For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > December 2005 > Import .sql file via PHP









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Import .sql file via PHP
Jamie Meyers

2005-05-30, 3:56 am

Does anyone know how to import a .sql dump file into a mysql database using
php. I know how to do it using commandline, mysql < test.sql, but I am
writing an install script, and was wondering if it is possible to do it this
way. If not, do you know of any parser that will do all the importing/table
creation the .sql file defines? Any response is much appreciated.

Thanks,
Jamie


Stephen Harris

2005-05-30, 8:56 am


"Jamie Meyers" <gtg061q@mail.gatech.edu> wrote in message
news:d7e8ge$26g$1@news-int2.gatech.edu...
> Does anyone know how to import a .sql dump file into a mysql database
> using php. I know how to do it using commandline, mysql < test.sql, but I
> am writing an install script, and was wondering if it is possible to do it
> this way. If not, do you know of any parser that will do all the
> importing/table creation the .sql file defines? Any response is much
> appreciated.
>
> Thanks,
> Jamie


I saw these instructions in the install text for PBS, an AMP program.

"Now we put data in our fresh database. In directory pbs/dump is a dump with
example data. Use mysql dbname < pbssample_english.dmp.
(dbname is $db_name in environment.php3)"

I think the dump delivered database structure for a help desk program,
PBS. I don't know much about this so you got "any response".

Another example:
a..
1.. go to the directory where the command mysql is recognized and type the
commands displayed in bold below:
a.. prompt$ mysql
b.. mysql> create database allonto;
c.. mysql> quit
d.. prompt$ mysql allonto < allonto.dmp

1.. Run the following command to dump your Mysql database:
2.. mysqldump -f -t -n >bacula-backup.dmp>


a..


Jamie Meyers

2005-05-30, 8:56 am

Nevermind, I decided to parse the .sql file and create the query there. I
do not think there is any way to import the .sql file via php. Thanks
anyways. And if anyone would like this script after I fix it up a little
more, let me know.

Jamie

"Stephen Harris" <cyberguard1048-usenet@yahoo.com> wrote in message
news:Iuyme.257$IE7.16@newssvr21.news.prodigy.com...
>
> I saw these instructions in the install text for PBS, an AMP program.
>
> "Now we put data in our fresh database. In directory pbs/dump is a dump
> with example data. Use mysql dbname < pbssample_english.dmp.
> (dbname is $db_name in environment.php3)"
>
> I think the dump delivered database structure for a help desk program,
> PBS. I don't know much about this so you got "any response".
>
> Another example:
> a..
> 1.. go to the directory where the command mysql is recognized and type
> the commands displayed in bold below:
> a.. prompt$ mysql
> b.. mysql> create database allonto;
> c.. mysql> quit
> d.. prompt$ mysql allonto < allonto.dmp
>
> 1.. Run the following command to dump your Mysql database:
> 2.. mysqldump -f -t -n >bacula-backup.dmp>
>
>
> a..
>
>



Anonymous

2005-05-30, 8:56 am

Jamie Meyers wrote:
>
> Does anyone know how to import a .sql dump file into a mysql database using
> php. I know how to do it using commandline, mysql < test.sql, but I am
> writing an install script, and was wondering if it is possible to do it this
> way. If not, do you know of any parser that will do all the importing/table


If you know how to do it with a commandline what's wrong with
exec("mysql < test.sql"); ?
Tim Van Wassenhove

2005-05-30, 8:56 am

On 2005-05-30, Jamie Meyers <gtg061q@mail.gatech.edu> wrote:
> Nevermind, I decided to parse the .sql file and create the query there. I
> do not think there is any way to import the .sql file via php. Thanks
> anyways. And if anyone would like this script after I fix it up a little
> more, let me know.


Assuming each instruction is on a line.. (untested)

$db = mysql_connect(....);
mysql_select_db(....);

$fp = fopen('somefile.sql', 'r');
while($fp != feof())
{
$line = fread($fp, 2048);
$line = mysql_real_escape_string($db, $line);
mysql_query($line);
}
fclose($fp);


--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>
Ivan Omelchenko 608308824

2005-05-30, 3:56 pm

Anonymous пишет:
> Jamie Meyers wrote:
>
>
>
> If you know how to do it with a commandline what's wrong with
> exec("mysql < test.sql"); ?

can be denied on public hostings!
Ivan Omelchenko 608308824

2005-05-30, 3:56 pm

Tim Van Wassenhove ÐÉÛÅÔ:
> On 2005-05-30, Jamie Meyers <gtg061q@mail.gatech.edu> wrote:
>
>
>
> Assuming each instruction is on a line.. (untested)
>
> $db = mysql_connect(....);
> mysql_select_db(....);
>
> $fp = fopen('somefile.sql', 'r');
> while($fp != feof())
> {
> $line = fread($fp, 2048);
> $line = mysql_real_escape_string($db, $line);
> mysql_query($line);
> }
> fclose($fp);
>
>

I guess it will NOT working, because of line can be much more than 2048
bytes, or it can be BLOB or another..
Or, can be comment multi line. your script will failed.
Tim Van Wassenhove

2005-05-30, 3:56 pm

On 2005-05-30, Ivan Omelchenko 608308824 <news@omelchenko.com> wrote:
> Tim Van Wassenhove ÐÉÛÅÔ:


[color=darkred]
> I guess it will NOT working, because of line can be much more than 2048
> bytes, or it can be BLOB or another..


That is up to the OP to find out what his needs are..


> Or, can be comment multi line. your script will failed.


That wouldn't meet the "each instruction is on a line" requirement..


Meaby a file_get_contents and mysqli_multiy_query are better suited for
the task..


--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jamie Meyers

2005-05-31, 3:58 am

If you have ever seen a mysql dump, it is just a dump of all the tables, and
all the data in the tables, unless specified otherwise. Everything is
broken up into multiple lines. A simple while statement worked for me, but
i just need to finish the error checking of the script. I will post it when
I am done.

Jamie


Alistair K

2005-12-13, 10:49 pm

quote:
Originally posted by Jamie Meyers
If you have ever seen a mysql dump, it is just a dump of all the tables, and
all the data in the tables, unless specified otherwise. Everything is
broken up into multiple lines. A simple while statement worked for me, but
i just need to finish the error checking of the script. I will post it when
I am done.

Jamie



Just wondering how you were getting along with that script. I've just started with MySQL and PHP and my first problem is exactly the one that you had when you started this thread. My server doesn't seem to let me run mysql directly, only via scripts, so your finished script would be very much appreciated!

I had some trouble trying to adapt it myself: the mysql_real_escape_string function generated an error message, also when replaced by its mysqli successor. Any suggestions about how to get around that? (Bear in mind that I'm a newbie, and easy to confuse.)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com