Solved

Dumping a MySQL datavase with php

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…

734 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