Jump to content

Archived

This topic is now archived and is closed to further replies.

craygo

.sql file

Recommended Posts

I am creating an application to parse a text file and import it into MySQL. What I want to add to the app is a section which will create the tables and fields for the import. I have an .sql file but do not know how to run it in php. Can anyone point me in the right direction to run the sql file with php, sort of how phpmyadmin does.

Thanks

Ray

Share this post


Link to post
Share on other sites
The only way I can think of is to explode on the ";" at end of each query then
[code]
$queries = explode (';', file_get_contents('my.sql'));
foreach ($queries as $sql) mysql_query(trim($sql));[/code]

Share this post


Link to post
Share on other sites
You'd also need to write some code to filter out the crap like:
[quote]--
-- Table structure for table `users`
-- [/quote]
Of which I guess could be done with something like:
[code]<?php
$new = "";
$file = file("dump.sql");
foreach($file as $l) {
    if(substr($l,0,2) != "--") $new .= $l."\r\n";
}
$h = fopen("stripped.sql","wb");
fwrite($h,$new);
fclose($h);
?>[/code]

Share this post


Link to post
Share on other sites
[quote]if(substr($l,0,2) != "--") $new .= $l."\r\n";[/quote]

You don't need to add the "\r\n" as they will already be there. file() doesn't strip them.

Share this post


Link to post
Share on other sites
Thanks guys! Got it to work. Since this is my own sql file, I took out all the crap. ie comments and other stuff. Also changed barands code a little to get it to work but it did the job. You got me on the right track.

Thanks again to both of you.

I LOVE THIS PLACE!!!!  ;D

Ray

Share this post


Link to post
Share on other sites
[quote author=Barand link=topic=101730.msg402850#msg402850 date=1153771454]
[quote]if(substr($l,0,2) != "--") $new .= $l."\r\n";[/quote]

You don't need to add the "\r\n" as they will already be there. file() doesn't strip them.
[/quote]Right you are - I don't always think before I type :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.