spacepoet Posted March 6, 2011 Share Posted March 6, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/229815-export-sql-database/ Share on other sites More sharing options...
redarrow Posted March 6, 2011 Share Posted March 6, 2011 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229815-export-sql-database/#findComment-1183736 Share on other sites More sharing options...
redarrow Posted March 6, 2011 Share Posted March 6, 2011 even better, sends the lot, to the user to there email adress http://www.sematopia.com/2006/02/how-to-backup-mysql-database-email-results-using-php/ you download this as help code, not tested via me google helped. Quote Link to comment https://forums.phpfreaks.com/topic/229815-export-sql-database/#findComment-1183737 Share on other sites More sharing options...
spacepoet Posted March 6, 2011 Author Share Posted March 6, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/229815-export-sql-database/#findComment-1183742 Share on other sites More sharing options...
redarrow Posted March 6, 2011 Share Posted March 6, 2011 i got it from here, remember you work with them or us helping you they wrote it ok. there a download page aswell ok. http://davidwalsh.name/backup-mysql-database-php sorry not tested it only read the comments. Quote Link to comment https://forums.phpfreaks.com/topic/229815-export-sql-database/#findComment-1183745 Share on other sites More sharing options...
spacepoet Posted March 6, 2011 Author Share Posted March 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229815-export-sql-database/#findComment-1183759 Share on other sites More sharing options...
redarrow Posted March 7, 2011 Share Posted March 7, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/229815-export-sql-database/#findComment-1183770 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.