Solved

Dumping a MySQL datavase with php

Posted on 2006-07-09
2
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
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.

623 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