Imaulle Posted January 28, 2011 Share Posted January 28, 2011 Hello, I'm having some issues with the following: $file = fopen('dump.sql', 'r'); $temp = ''; while($line = fgets($file)) { if ((substr($line, 0, 2) != '--') && (substr($line, 0, 2) != '/*') && (substr($line,0,1) != '#') && (strlen($line) > 1)) { $last = trim(substr($line, -2, 1)); $temp .= trim(substr($line, 0, -1)); if ($last == ';') { mysql_query($temp) or die(mysql_error()); $temp = ''; } } } fclose($file); the sql files are either phpmyadmin dumps or heidi sql dumps. I'm not getting any error from the error() call but nothing is ever imported into the database. I'm able to login to phpMyAdmin and import the file without any issues but I'd really like to stay out of the cPanel/phpmyadmin as much as possible. what am I missing? thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/225923-mysql-dump/ Share on other sites More sharing options...
requinix Posted January 28, 2011 Share Posted January 28, 2011 Are you sure the dump.sql isn't using Windows-style line endings? If you've compensated for that, well... you haven't. Not quite. Try the trim() a bit earlier. Like $file = fopen('dump.sql', 'r'); $temp = ''; while($line = fgets($file)) { $line = trim($line); if ((substr($line, 0, 2) != '--') && (substr($line, 0, 2) != '/*') && (substr($line,0,1) != '#') && (strlen($line) > 1)) { $last = substr($line, -1); $temp .= rtrim(substr($line, 0, -1)); if ($last == ';') { mysql_query($temp) or die(mysql_error()); $temp = ''; } } } fclose($file); And you're sure that every line contains exactly one, complete command or comment? Quote Link to comment https://forums.phpfreaks.com/topic/225923-mysql-dump/#findComment-1166410 Share on other sites More sharing options...
Imaulle Posted January 28, 2011 Author Share Posted January 28, 2011 okay I applied the changes you made and I get the following error now: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-- phpMyAdmin SQL DumSET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"' at line 1 the commands are not on single lines, most of them take up many many lines... Quote Link to comment https://forums.phpfreaks.com/topic/225923-mysql-dump/#findComment-1166414 Share on other sites More sharing options...
Imaulle Posted January 28, 2011 Author Share Posted January 28, 2011 ohhhh I see what is happening now... there must not be any end of line characters right? hmmmm... Quote Link to comment https://forums.phpfreaks.com/topic/225923-mysql-dump/#findComment-1166425 Share on other sites More sharing options...
Imaulle Posted January 29, 2011 Author Share Posted January 29, 2011 the first characters of the file when I view it in notepad are '--' but when I output the first few chars of $file its some weird symbols and the '--' doesn't start till 4 chars into the file. Is there a function I need to call to remove these special characters at the beginning of the file? Quote Link to comment https://forums.phpfreaks.com/topic/225923-mysql-dump/#findComment-1166906 Share on other sites More sharing options...
Pikachu2000 Posted January 29, 2011 Share Posted January 29, 2011 It sounds like you may have opened it in Notepad and re-saved it, and when you did that it got a BOM (byte-order mark) added. Save it as UTF-8 without BOM. Quote Link to comment https://forums.phpfreaks.com/topic/225923-mysql-dump/#findComment-1167037 Share on other sites More sharing options...
Imaulle Posted February 7, 2011 Author Share Posted February 7, 2011 I only use programmer's notepad and it said it was saved as 'ANSI' I converted it to UTF-8 and that seems to have fixed the issue. Does everything I do need to be saved as UTF-8 ? Quote Link to comment https://forums.phpfreaks.com/topic/225923-mysql-dump/#findComment-1171151 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.