Sunteți pe pagina 1din 8

PHP Server Side Scripting

New! Try this search on WebmasterWorld Site Search

These terms have been highlighted:


sql dump php [ remove highlighting]

PHP/MySQL - a command to write an SQL database dump file?

MTKilpatrick

#:1247883 4:54 pm on Feb 16, 2005 (utc 0)

I'm really confused by the various backup methods for Mysql. I'm only interested in having a
manually generated backup in the form of an sql file containing a series of "DROP
TABLE...CREATE TABLE...INSERT" statements, which I can reload if necessary. I can do this
from within phpMyAdmin, but I'd really like to know how to do this myself from within my
Apache/php-based website code on my home PC so that I can create the text file at the push of a
button. I'm writing some php code to manage some data via a web interface. I can't seem to find in
the docs exactly how to do this except from the Mysql server command line using mysqldump.
But how do I do this from within a php program, please?

Michael

RussellC

#:1247884 5:36 pm on Feb 16, 2005 (utc 0)

You can run the mysqldump command in php via the system() function.

hakre

#:1247885 1:33 am on Feb 17, 2005 (utc 0)

checkout the sources of phpmyadmin, it's gpl and freely available. one part of phpmyadmin is
creating such kind of data, myabe by using mysqldump for this - i don't exactly know. but in this
code you will find much more information.

frobo
#:1247886 11:16 pm on Feb 18, 2005 (utc 0)

How did you go?


did you come up with a good solution?

jatar_k

#:1247887 11:18 pm on Feb 18, 2005 (utc 0)

Welcome to WebmasterWorld frobo

frobo

#:1247888 5:01 am on Mar 2, 2005 (utc 0)

Thanks,
I found a good one:

function get_structure()
{
$server = $DBADDRESS;
$user = $DBUSERNAME;
$pass = $DBPASSWORD;
$db = $DB;

mysql_connect($server, $user, $pass);


mysql_select_db($db);
$tables = mysql_list_tables($db);
while ($td = mysql_fetch_array($tables))
{
$table = $td[0];
$r = mysql_query("SHOW CREATE TABLE `$table`");
if ($r)
{
$insert_sql = "";
$d = mysql_fetch_array($r);
$d[1] .= ";";
$sql[] = str_replace("\n", "", $d[1]);
$table_query = mysql_query("SELECT * FROM `$table`");
$num_fields = mysql_num_fields($table_query);
while ($fetch_row = mysql_fetch_array($table_query))
{
$insert_sql .= "INSERT INTO $table VALUES(";
for ($n=1;$n<=$num_fields;$n++)
{
$m = $n - 1;
$insert_sql .= "'".mysql_real_escape_string($fetch_row[$m])."', ";
}
$insert_sql = substr($insert_sql,0,-2);
$insert_sql .= ");\n";
}
if ($insert_sql!= "")
{
$sql[] = $insert_sql;
}
}
}
return implode("\r", $sql);
}

Home / Forums Index / The Webmaster World / PHP Server Side Scripting

S-ar putea să vă placă și