Solved

Dumping a MySQL datavase with php

Posted on 2006-07-09
2
333 Views
Last Modified: 2013-12-12
I'm looking for a way to move a MySQL database from one web derver to another.

Through a Google search I found MySQL Dump Class. It looks like it might work, but the example (as follows) is TOTALLY confusing. I want to save the dumped database to a file that I can ftp to my local machine & upload to a new host.

This "example" script seems like it "prints" the dump? Why on earth do that? Also, there is no reference to "mydb". What is that?

<?php

mysql_connect("localhost", "root", "");

require("class_mysqldump.php");
$dump = new MySQLDump();

print $dump->dumpDatabase("mydb");

?>

Can someone help me understand this example script or point me to another way? I want to dump the MySQL database to a file. I ONLY have ftp access & the current web host is uncooperative. I DO NO have general command line access at the server so I don't think I can use MySQL Dump.

Thanks
0
Comment
Question by:Richard Korts
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 17069200
> I want to dump the MySQL database to a file
well, simply write to a file instead of printing to the screen:
http://php.net/fwrite

here a complete script:

mysql_connect("localhost", "root", "");
mysql_select_db("database_name");

require("class_mysqldump.php");
$dump = new MySQLDump();

$data = $dump->dumpDatabase("mydb");

$filename = 'test.txt';

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }

   // Write $somecontent to our opened file.
   if (fwrite($handle, $data) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
 
   echo "Success, wrote database dump to file ($filename)";
 
   fclose($handle);

} else {
   echo "The file $filename is not writable";
}
0
 

Author Comment

by:Richard Korts
ID: 17069771
To AngelIII

Worked like a charm!

Thanks,

Rkorts
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question