Jump to content

save to file dont work


web_master

Recommended Posts

hi,

 

can someone tel me why this script work on my local machine, and dont work on server (internet)... :(

 

thnxs

 

<?php 
mysql_connect("localhost", "user", "password");

require("class_mysqldump.php");
$dump = new MySQLDump();
$myString = $dump->dumpDatabase("database");


$content_len = count($myString);
$filename = date("Ymd")."_npn_dBase";
$output_file = $filename . ".sql";
header("Content-Disposition: attachment; filename=" . $output_file);
header("Content-Type: text/html");
header('Content-Length: ' . $content_len);
//echo $myString;
exit();
?>

 

 

this is the class

 

<?php

class MySQLDump {


/**
 * Dump data and structure from MySQL database
 *
 * @param string $database
 * @return string
 */
function dumpDatabase($database) {

	// Set content-type and charset
	header ('Content-Type: text/html; charset=iso-8859-1');

	// Connect to database
	$db = @mysql_select_db($database);

	if (!empty($db)) {

		// Get all table names from database
		$c = 0;
		$result = mysql_list_tables($database);
		for($x = 0; $x < mysql_num_rows($result); $x++) {
			$table = mysql_tablename($result, $x);
			if (!empty($table)) {
				$arr_tables[$c] = mysql_tablename($result, $x);
				$c++;
			}
		}

		// List tables
		$dump = '';
		for ($y = 0; $y < count($arr_tables); $y++){

			// DB Table name
			$table = $arr_tables[$y];

			// Structure Header
			$structure .= "-- \n";
			$structure .= "-- Table structure for table `{$table}` \n";
			$structure .= "-- \n\n";

			// Dump Structure
			$structure .= "DROP TABLE IF EXISTS `{$table}`; \n";
			$structure .= "CREATE TABLE `{$table}` (\n";
			$result = mysql_db_query($database, "SHOW FIELDS FROM `{$table}`");
			while($row = mysql_fetch_object($result)) {

				$structure .= "  `{$row->Field}` {$row->Type}";
				$structure .= (!empty($row->Default)) ? " DEFAULT '{$row->Default}'" : false;
				$structure .= ($row->Null != "YES") ? " NOT NULL" : false;
				$structure .= (!empty($row->Extra)) ? " {$row->Extra}" : false;
				$structure .= ",\n";

			}

			$structure = ereg_replace(",\n$", "", $structure);

			// Save all Column Indexes in array
			unset($index);
			$result = mysql_db_query($database, "SHOW KEYS FROM `{$table}`");
			while($row = mysql_fetch_object($result)) {

				if (($row->Key_name == 'PRIMARY') AND ($row->Index_type == 'BTREE')) {
					$index['PRIMARY'][$row->Key_name] = $row->Column_name;
				}

				if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '0') AND ($row->Index_type == 'BTREE')) {
					$index['UNIQUE'][$row->Key_name] = $row->Column_name;
				}

				if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'BTREE')) {
					$index['INDEX'][$row->Key_name] = $row->Column_name;
				}

				if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'FULLTEXT')) {
					$index['FULLTEXT'][$row->Key_name] = $row->Column_name;
				}

			}

			// Return all Column Indexes of array
			if (is_array($index)) {
				foreach ($index as $xy => $columns) {

					$structure .= ",\n";

					$c = 0;
					foreach ($columns as $column_key => $column_name) {

						$c++;

						$structure .= ($xy == "PRIMARY") ? "  PRIMARY KEY  (`{$column_name}`)" : false;
						$structure .= ($xy == "UNIQUE") ? "  UNIQUE KEY `{$column_key}` (`{$column_name}`)" : false;
						$structure .= ($xy == "INDEX") ? "  KEY `{$column_key}` (`{$column_name}`)" : false;
						$structure .= ($xy == "FULLTEXT") ? "  FULLTEXT `{$column_key}` (`{$column_name}`)" : false;

						$structure .= ($c < (count($index[$xy]))) ? ",\n" : false;

					}

				}

			}

			$structure .= "\n);\n\n";

			// Header
			$structure .= "-- \n";
			$structure .= "-- Dumping data for table `$table` \n";
			$structure .= "-- \n\n";

			// Dump data
			unset($data);
			$result     = mysql_query("SELECT * FROM `$table`");
			$num_rows   = mysql_num_rows($result);
			$num_fields = mysql_num_fields($result);

			for ($i = 0; $i < $num_rows; $i++) {

				$row = mysql_fetch_object($result);
				$data .= "INSERT INTO `$table` (";

				// Field names
				for ($x = 0; $x < $num_fields; $x++) {

					$field_name = mysql_field_name($result, $x);

					$data .= "`{$field_name}`";
					$data .= ($x < ($num_fields - 1)) ? ", " : false;

				}

				$data .= ") VALUES (";

				// Values
				for ($x = 0; $x < $num_fields; $x++) {
					$field_name = mysql_field_name($result, $x);

					$data .= "'" . str_replace('\"', '"', mysql_escape_string($row->$field_name)) . "'";
					$data .= ($x < ($num_fields - 1)) ? ", " : false;

				}

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

			$data.= "\n";

			$dump .= $structure . $data;
			$dump .= "-- --------------------------------------------------------\n\n";

		}

		return $dump;

	}

}

}

?>

Link to comment
Share on other sites

Your code has little to no error checking, no error reporting (it in fact is suppressing errors at one point using an @), no error logging (optional for learning type applications, required for real life applications), and no error recovery logic.

 

Any of your php function calls could be failing and your code blindly continues execution. You need to read the function definitions in the php manual and add appropriate error checking logic to find out at what is working and what is failing.

 

Short answer - your code has no logic in it to tell you why it is not working and we can't tell you exactly why it is failing either.

 

You can add the following two lines immediately after your first opening <?php tag to get any php produced errors to be displayed -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

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.