craygo Posted July 24, 2006 Share Posted July 24, 2006 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.ThanksRay Quote Link to comment https://forums.phpfreaks.com/topic/15533-sql-file/ Share on other sites More sharing options...
Barand Posted July 24, 2006 Share Posted July 24, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/15533-sql-file/#findComment-63100 Share on other sites More sharing options...
zq29 Posted July 24, 2006 Share Posted July 24, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/15533-sql-file/#findComment-63104 Share on other sites More sharing options...
Barand Posted July 24, 2006 Share Posted July 24, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/15533-sql-file/#findComment-63111 Share on other sites More sharing options...
craygo Posted July 24, 2006 Author Share Posted July 24, 2006 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!!!! ;DRay Quote Link to comment https://forums.phpfreaks.com/topic/15533-sql-file/#findComment-63112 Share on other sites More sharing options...
zq29 Posted July 24, 2006 Share Posted July 24, 2006 [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 :) Quote Link to comment https://forums.phpfreaks.com/topic/15533-sql-file/#findComment-63118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.