Jump to content

Export .sql database ??


spacepoet

Recommended Posts

Hi:

 

I think this would pertain to a PHP coding feature.

 

I want to give a user the ability to click a link in the admin area called "Export Database" the will export the entire database like: myDataBase.sql

 

and allow them to save it to their computer.

 

They want to be able to make backups without logging into a PHP admin area.

 

I use to do this with Access, but have not done this with mySQL.

 

Has anyone done this before?

Link to comment
https://forums.phpfreaks.com/topic/229815-export-sql-database/
Share on other sites

The rest is easy you do the rest, yes a example from google i like to rest.

 

Here a good example try it.

 

Alter the way you need it.

 

 

 


<?php

backup_tables('localhost','username','password','blog');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);

//get all of the tables
if($tables == '*')
{
	$tables = array();
	$result = mysql_query('SHOW TABLES');
	while($row = mysql_fetch_row($result))
	{
		$tables[] = $row[0];
	}
}
else
{
	$tables = is_array($tables) ? $tables : explode(',',$tables);
}

//cycle through
foreach($tables as $table)
{
	$result = mysql_query('SELECT * FROM '.$table);
	$num_fields = mysql_num_fields($result);

	$return.= 'DROP TABLE '.$table.';';
	$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
	$return.= "\n\n".$row2[1].";\n\n";

	for ($i = 0; $i < $num_fields; $i++) 
	{
		while($row = mysql_fetch_row($result))
		{
			$return.= 'INSERT INTO '.$table.' 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])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
				if ($j<($num_fields-1)) { $return.= ','; }
			}
			$return.= ");\n";
		}
	}
	$return.="\n\n\n";
}

//save file
$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}

?>

Link to comment
https://forums.phpfreaks.com/topic/229815-export-sql-database/#findComment-1183736
Share on other sites

Hi:

 

Thanks for the response - I'm working on your first post.

 

I did it like this:

<a href="a_Export.php">Export</a>

 

a_Export.php

<?php
include('../include/myConn.php');
backup_tables('bob.db.73909.hosted.com','bob','Rob','blog');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

etc...

 

I only added the DB connection, and the host, user, password, info.

 

Problem is, if I click the "Export" link, the page is blank. No errors; but a blank page.

 

Do I need to alter any of the other code? Like add TABLE names?

 

Sorry, I just haven't done this before so I don't know where to go with it.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/229815-export-sql-database/#findComment-1183742
Share on other sites

Wait! OK, it is working hoe I posted it! Nice...

 

I didn't realize it was writing it onto the web hosting account.

 

Do you know how I can adjust it so it will open as a "Save As..." prompt box, so the client

can save it onto his local computer.

 

That is what he would like.

 

Thanks for showing me this.

Link to comment
https://forums.phpfreaks.com/topic/229815-export-sql-database/#findComment-1183759
Share on other sites

Somethink like this .....

 

i am no good with javascript, so someone elese have to help sorry

 

<script type="text/javascript">
<!--
function save(){
str = document.forms[0].t1.value;
mydoc = document.open();
mydoc.write(str);
mydoc.execCommand("saveAs",true,".txt");
mydoc.close();
return false;
}
//-->
</script>
</head>

<body class="body">
<form id="form1" action="" onsubmit="">
<input type="text" name="t1"/>
<input type="button" value="process" onclick="save()"/>
</form>

Link to comment
https://forums.phpfreaks.com/topic/229815-export-sql-database/#findComment-1183770
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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