PHP tutorial export. sql file/mysql Tutorial Database Tutorial Backup Program
$database = ";//Database name
$options =array (
' Hostname ' = ', '//IP address
' CharSet ' = ' utf8 ',//coded
' filename ' = ' $database. ' SQL ',//file name
' Username ' = ',
' Password ' and ' = '
);
mysql_connect ($options [' hostname '], $options [' username '], $options [' Password ']) or Die ("Cannot connect to database!");
mysql_select_db ($database) or Die ("database name is wrong!");
mysql_query ("SET NAMES ' {$options [' CharSet ']} '");
$tables = List_tables ($database);
$filename = sprintf ($options [' filename '], $database);
$fp = fopen ($filename, ' w ');
foreach ($tables as $table) {
Dump_table ($table, $fp);
}
Fclose ($FP);
Download SQL file
$file _name= $options [' filename '];
Header ("Content-type:application/octet-stream");
Header ("Content-disposition:attachment;filename=". $file _name);
ReadFile ($file _name);
Delete the SQL file on the server
Unlink ($file _name);
Exit
Get the name of the table
function List_tables ($database)
{
$rs = Mysql_list_tables ($database);
$tables = Array ();
while ($row = Mysql_fetch_row ($rs)) {
$tables [] = $row [0];
}
Mysql_free_result ($RS);
return $tables;
}
Export Database
function dump_table ($table, $fp = null)
{
$need _close = false;
if (Is_null ($fp)) {
$fp = fopen ($table. '. sql ', ' W ');
$need _close = true;
}
$a =mysql_query ("show create TABLE ' {$table} '");
$row =MYSQL_FETCH_ASSOC ($a); Fwrite ($fp, $row [' Create Table ']. '; '); /export Table structure
$rs = mysql_query ("select * from ' {$table} ');
while ($row = Mysql_fetch_row ($rs)) {
Fwrite ($fp, Get_insert_sql ($table, $row));
}
Mysql_free_result ($RS);
if ($need _close) {
Fclose ($FP);
}
}
Export Table Data
function Get_insert_sql ($table, $row)
{
$sql = "INSERT into ' {$table} ' VALUES (";
$values = Array ();
foreach ($row as $value) {
$values [] = "'". Mysql_real_escape_string ($value). "'";
}
$sql. = Implode (', ', $values). ");";
return $sql;
}?>
http://www.bkjia.com/PHPjc/630883.html www.bkjia.com true http://www.bkjia.com/PHPjc/630883.html techarticle PHP Tutorial Export. sql file/mysql Tutorial Database tutorial Backup program? php $database = ';//Database name $options =array (' hostname ' = ',//ip address ' charset ' = ' utf8 ',/ /encode ' filename ' ...