By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,409 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,409 IT Pros & Developers. It's quick & easy.

Import .sql file via PHP

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.