|
Posted by Littlefire on 06/13/06 06:01
laredotornado@zipmail.com wrote:
> Hello,
>
> I'm using PHP 4, MySQL 4. I have a text file where each line contains
> an SQL command. Is there a way, using PHP, to execute the contents of
> the file without parsing through each line of the file (using fopen and
> fgets) and executing each command one by one?
Yes. Here's some pseudocode:
Open file from disk
Read in contents of the entire file
Replace all newlines with ";\n"
Send the entire file as a mysql_query()
The rationale is that you can send many commands to MySQL at once, provided
that they're separated with end-of-command characters which happens to be a
semicolon, ";". This approach will be limited by the PHP RAM buffer as well
as the MySQL command line buffer. If you have a file measuring a few MB in
size, it would be easier to modify the file into proper MySQL commands with
something like sed, or any other commandline text replacement tool (Perl?),
and then feed the file directly to MySQL:
exec("sed 's/\n/\;\n/' wrong.file > file.ext");
exec("for i in `cat file.ext`; do mysql -uusername -ppassword dbname -e $i;
done");
Now your script will be limited by the PHP execution time limit, so to get
rid of these constraints, just type the commands on the commandline.
I don't think all those commands will work on Windows, but not sure (the
last time I used Windows was when it was a GUI for DOS. Ooops, it still
is :) )
HTH,
A
[Back to original message]
|