Solved

Dumping a MySQL datavase with php

Posted on 2006-07-09
2
327 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now