472,145 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Import .sql file via PHP

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
Jul 17 '05 #1
8 25314

"Jamie Meyers" <gt*****@mail.gatech.edu> wrote in message
news:d7**********@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..
Jul 17 '05 #2
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" <cy*******************@yahoo.com> wrote in message
news:Iu**************@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..

Jul 17 '05 #3
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"); ?
Jul 17 '05 #4
On 2005-05-30, Jamie Meyers <gt*****@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>
Jul 17 '05 #5
Anonymous пишет:
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"); ?

can be denied on public hostings!
Jul 17 '05 #6
Tim Van Wassenhove ÐÉÛÅÔ:
On 2005-05-30, Jamie Meyers <gt*****@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);

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.
Jul 17 '05 #7
On 2005-05-30, Ivan Omelchenko 608308824 <ne**@omelchenko.com> wrote:
Tim Van Wassenhove ÐÉÛÅÔ:
Assuming each instruction is on a line.. (untested)

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>
Jul 17 '05 #8
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
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Stian Søiland | last post: by
6 posts views Thread by bry | last post: by
5 posts views Thread by Steve Holden | last post: by
1 post views Thread by Learning Python | last post: by
2 posts views Thread by David Berry | last post: by
16 posts views Thread by didier.doussaud | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.