Jump to content


Photo

.sql file


  • Please log in to reply
5 replies to this topic

#1 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 24 July 2006 - 07:41 PM

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 24 July 2006 - 07:50 PM

The only way I can think of is to explode on the ";" at end of each query then
$queries = explode (';', file_get_contents('my.sql'));
foreach ($queries as $sql) mysql_query(trim($sql));

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 24 July 2006 - 07:58 PM

You'd also need to write some code to filter out the crap like:

--
-- Table structure for table `users`
--

Of which I guess could be done with something like:
<?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);
?>


#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 24 July 2006 - 08:04 PM

if(substr($l,0,2) != "--") $new .= $l."\r\n";


You don't need to add the "\r\n" as they will already be there. file() doesn't strip them.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 24 July 2006 - 08:05 PM

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

#6 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 24 July 2006 - 08:09 PM

if(substr($l,0,2) != "--") $new .= $l."\r\n";


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

Right you are - I don't always think before I type :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users