Jump to content


mySQL Export Script Not Working


8 replies to this topic

#1 JamesH

    Young Padawan

  • Members
  • Pip
  • 13 posts

Posted 14 November 2006 - 01:01 PM

Hey

Firstly, I will show you my codes:

index.php:
<?php
include 'mysql.inc.php';
$mysql = new mysql;
$mysql->connect(****,****,****);
$tables = array('banned');
$mysql->export(smf,$tables);
?>

mysql.inc.php:
<?php
class mysql {
	function export($db,$tcable) {
		mysql_select_db($db) or die(mysql_error());
		foreach($table as $t) {
				$query = mysql_query("select * from $t") or die(mysql_error());
				$num_fields = mysql_num_fields($query) or die(mysql_error());
				$numrow = mysql_num_rows($query) or die(mysql_error());
				for ($i =0; $i<$numrow; $i++) {
					$result .= "INSERT INTO ".$t." VALUES(";

					for($j=0; $j<$num_fields; $j++) {
						$row[$j] = addslashes($row[$j]);
						$row[$j] = ereg_replace("\n","\\n",$row[$j]);

						if (isset($row[$j])) {
							$result .= "\"$row[$j]\"";
						} else {
							$result .= "\"\"";
						}

						if ($j<($num_fields-1)) $result .= ",";

					}
				$result .= ");\n";
				}
		}

		$file_name = "MySQL_Database_Backup.sql";
		header("Content-type: application/octet-stream");
		header("Content-Disposition: attachment; filename=$file_name");
		return $result;
	}
}
?>

And if I run index.php, I get nothing. The script doesn't actually exit, but I just get no response.

However, if I change

$tables = array('banned');

to..

$tables = array();

.. I will get the file back, but obviously it is empty.


Any help would be great!

Thanks,
James.

Edited by JamesH, 14 November 2006 - 01:01 PM.


#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 14 November 2006 - 02:00 PM

try:

$tables = array();
$tables[] = 'banned';

Matt

#3 JamesH

    Young Padawan

  • Members
  • Pip
  • 13 posts

Posted 14 November 2006 - 02:10 PM

That's not working :lol:

#4 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 14 November 2006 - 03:17 PM

View PostMr. Matt, on Nov 14 2006, 07:00 PM, said:

try:

$tables = array();
$tables[] = 'banned';

Matt

[] creates a new array (if non-existent) anyway so no need for the first line :lol:

View PostJamesH, on Nov 14 2006, 07:10 PM, said:

That's not working :D

Dang. Well i don't know why it's not working...only thing i see wrong is:
				$num_fields = mysql_num_fields($query) or die(mysql_error());
				$numrow = mysql_num_rows($query) or die(mysql_error());

// should be...

				$num_fields = mysql_num_fields($query);
				$numrow = mysql_num_rows($query);

Whether that would cause a problem is debatable...

(sorry for not replying again on TT btw, completely slipped my mind.)

Edit, Matt before you say anything i delt i had to reply to this as i forgot on TT....I am on holiday lol it's just by chance i saw this.

Edited by Matthew., 14 November 2006 - 03:17 PM.


#5 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 14 November 2006 - 04:47 PM

lol don't worry I won't. And with my method, someone at work taught me it is always better to define the array just for security reasons.

AHA found it. 5th line:

foreach($table as $t) {

Change to:

foreach($tcables as $t) {

As you defined it above as:

$tables = array('banned');

=)

Edited by Mr. Matt, 14 November 2006 - 05:40 PM.


#6 JamesH

    Young Padawan

  • Members
  • Pip
  • 13 posts

Posted 15 November 2006 - 12:52 PM

Ok, I am now getting my file back but with no data in it. I think the "or die(mysql_error())" was stopping the function, but now I've got rid of it I can't debug the error ^_^

Here's my codes for an update:

	// to export (backup) a database
	function export($db,$tables) {
		mysql_select_db($db) or die(mysql_error());
		$result = "## Dump created ".date("d,M,Y")." ##";
		foreach($tables as $t) {
				$query = mysql_query("select * from $t");
				$num_fields = mysql_num_fields($query);
				$numrow = mysql_num_rows($query);
				for ($i =0; $i<$numrow; $i++) {
					$result .= "INSERT INTO ".$t." VALUES(";

					for($j=0; $j<$num_fields; $j++) {
						$row[$j] = addslashes($row[$j]);
						$row[$j] = ereg_replace("\n","\\n",$row[$j]);

						if (isset($row[$j])) {
							$result .= "\"$row[$j]\"";
						} else {
							$result .= "\"\"";
						}

						if ($j<($num_fields-1)) $result .= ",";

					}
				$result .= ");\n";
				}
		}

		$file_name = "MySQL_Database_Backup.sql";
		header("Content-type: application/octet-stream");
		header("Content-Disposition: attachment; filename=$file_name");
		print $result;
	}

and the function call is:

	$tables = array();
	$tables[] = 'banned';
	$mysql->export(smf,$tables);

Thanks for the help ^_^

Edited by JamesH, 15 November 2006 - 12:54 PM.


#7 JamesH

    Young Padawan

  • Members
  • Pip
  • 13 posts

Posted 15 November 2006 - 01:17 PM

I did this:

	// to export (backup) a database
	function export($db,$tables) {
		mysql_select_db($db) or die(mysql_error());
		$result = "## Dump created ".date("d M Y")." ##";
		foreach($tables as $t) {
				$query = mysql_query("select * from $t");
				$num_fields = mysql_num_fields($query);
				$numrow = mysql_num_rows($query);
				for ($i =0; $i<$numrow; $i++) {
					$result .= "INSERT INTO ".$t." VALUES(";

					for($j=0; $j<$num_fields; $j++) {
						$row[$j] = addslashes($row[$j]);
						$row[$j] = ereg_replace("\n","\\n",$row[$j]);

						if (isset($row[$j])) {
							$result .= "\"$row[$j]\"";
						} else {
							$result .= "\"\"";
						}

						if ($j<($num_fields-1)) $result .= ",";

					}
				$result .= ");\n";
				}
		}
		return die(mysql_error());
		#$file_name = "MySQL_Database_Backup.sql";
		#header("Content-type: application/octet-stream");
		#header("Content-Disposition: attachment; filename=$file_name");
		#print $result;
	}

..and didn't get any errors.

#8 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 15 November 2006 - 01:34 PM

well firstly nothing will be returned as you have told it to die(); and there is no SQL errors so it won't return an error.

#9 JamesH

    Young Padawan

  • Members
  • Pip
  • 13 posts

Posted 15 November 2006 - 01:37 PM

View Post. Adam ., on Nov 15 2006, 01:32 PM, said:

Try this:

	// to export (backup) a database
	function export($db, $tables) {

		mysql_select_db($db) or die(mysql_error());
		$result = "## Dump created ".date("d M Y")." ##";
		foreach($tables as $t) {
				$query = mysql_query("select * from $t") or die(mysql_error());
				$num_fields = mysql_num_fields($query);
				$numrow = mysql_num_rows($query);
				for ($i =0; $i<$numrow; $i++) {
					$result .= "INSERT INTO ".$t." VALUES(";

					for($j=0; $j<$num_fields; $j++) {
						$row[$j] = addslashes($row[$j]);
						$row[$j] = ereg_replace("\n","\\n",$row[$j]);

						if (isset($row[$j])) {
							$result .= "\"$row[$j]\"";
						} else {
							$result .= "\"\"";
						}

						if ($j<($num_fields-1)) $result .= ",";

					}
				$result .= ");\n";
				}
		}
		return die(mysql_error());
		#$file_name = "MySQL_Database_Backup.sql";
		#header("Content-type: application/octet-stream");
		#header("Content-Disposition: attachment; filename=$file_name");
		#print $result;
	}

Do you get any errors now?

- Adam ^_^

No ^_^





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users