Jump to content

.sql file


craygo

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
Link to comment
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]
Link to comment
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
Link to comment
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 :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.