473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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 25416

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
6
by: bry | last post by:
Hi, I'm trying to do a dynamic import of a file that has no problems with it, that is to say I can import it normally, my sys.path is set to the right folder etc. but my dynamic import code is not...
1
by: Andrew James | last post by:
All, I'm having some trouble with understanding python's importing behaviour in my application. I'm using psyco to optimise part of my code, but I'm not sure whether it inherits throughout the...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
1
by: Learning Python | last post by:
An example in the book I didn't understood well two modules files recursively import/from each other in recur1.py,we have: x=1 import recur2 y=1
2
by: David Berry | last post by:
Hi All. I'm looking for any help or sample code that can show me how to make a file import wizard in ASP.NET (VB preferred) like the one that MS Access uses. I'm working on a web site where the...
16
by: didier.doussaud | last post by:
I have a stange side effect in my project : in my project I need to write "gobal" to use global symbol : .... import math .... def f() : global math # necessary ?????? else next line...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
3
by: yinglcs | last post by:
Hi, i have 2 python files in *different directory* , how can I import python functions from 1 python file to another? i get this error: import task ImportError: No module named task/ Thank...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.