Jump to content

Recommended Posts

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!!

Link to comment
https://forums.phpfreaks.com/topic/225923-mysql-dump/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/225923-mysql-dump/#findComment-1166410
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/225923-mysql-dump/#findComment-1166414
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/225923-mysql-dump/#findComment-1166906
Share on other sites

  • 2 weeks later...
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.