Imaulle Posted August 27, 2010 Share Posted August 27, 2010 Hello, I'm having trouble with the following code $file = file_get_contents("database.sql"); $queries = explode(";",$file); for ($i=0,$c=count($queries);$i<$c;$i++){ mysql_query($queries[$i],$mlink); } the problem is the sql file I'm trying to import has ; alllll over the place, not just at the end of the queries, so the queries are not getting inputted correctly. I could see a way to fix this if each query was on a single line, but some of them are over 100 lines each. How do I make sure it only explodes using the ; at the end of query? and not the ; that are actually in the tables etc. thanks Quote Link to comment Share on other sites More sharing options...
Imaulle Posted August 27, 2010 Author Share Posted August 27, 2010 okay so I'm thinking I need to take in each line, and then check if the last character is a ; I'm not sure where to go from here though... Quote Link to comment Share on other sites More sharing options...
Imaulle Posted August 27, 2010 Author Share Posted August 27, 2010 oh snap, could I just do $queries = explode(";\n",$file); ??? Quote Link to comment Share on other sites More sharing options...
Adam Posted August 27, 2010 Share Posted August 27, 2010 Assuming none of the queries have a semi-colon followed by a line break in them. Have you tried it? Quote Link to comment Share on other sites More sharing options...
Alex Posted August 27, 2010 Share Posted August 27, 2010 Is there any particular reason you're attempting to import the SQL file this way as opposed to the more suitable method of utilizing a MySQL administration tool? Quote Link to comment Share on other sites More sharing options...
Imaulle Posted August 27, 2010 Author Share Posted August 27, 2010 all of the .sql files are phpMyAdmin dumps, so I assume all of the queries end with ; and then a line break ? this script is for doing a setup of a website, that I gotta install very very many times. I basically upload a zip file, an sql file, and a php file. I then run the php file which will unzip the website, make the sql db, user, add permissions and then a few other things related to the website files. I want to stay out of cPanel, this way is muuuuuuuuch faster Quote Link to comment Share on other sites More sharing options...
trq Posted August 27, 2010 Share Posted August 27, 2010 I'm with AlexWD, use mysql's cli interface, it'll be allot simpler and more efficient. Quote Link to comment Share on other sites More sharing options...
Imaulle Posted August 27, 2010 Author Share Posted August 27, 2010 it adds an extra 5 minutes or so that I don't have. I prefer to do it in the php script, if possible. Quote Link to comment Share on other sites More sharing options...
oliverj777 Posted August 27, 2010 Share Posted August 27, 2010 I've never done it through PHP before. Have you managed to find the problem yet or is it still printing loads of ;;; everywhere? Quote Link to comment Share on other sites More sharing options...
Imaulle Posted August 27, 2010 Author Share Posted August 27, 2010 as far as I can tell this works $file = file_get_contents("database.sql"); $queries = explode(";\n",$file); for ($i=0,$c=count($queries);$i<$c;$i++){ mysql_query($queries[$i],$mlink); } so I guess I can mark this solved unless someone can see an issue with it D: Quote Link to comment Share on other sites More sharing options...
oliverj777 Posted August 27, 2010 Share Posted August 27, 2010 I would imagine its something to do with your 'explode' and $file. The explode is splitting your string by string, and being whats in your $file - is causing it to repeat/place your ;; everywhere I'm no expert though Quote Link to comment 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.