小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

用PHP導(dǎo)出MySQL數(shù)據(jù)庫(kù)內(nèi)容為.sql文件

 wwwijhyt圖書館 2014-05-11

通常我們都用 phpMyAdmin 來(lái)導(dǎo)出,不過(guò)如果你的數(shù)據(jù)庫(kù)如果存在下列問(wèn)題,那么 phpMyAdmin 也無(wú)能為力。


數(shù)據(jù)庫(kù)的字符集與應(yīng)用程序的字符集不一致;
應(yīng)用程序用錯(cuò)誤的編碼將數(shù)據(jù)保存到了數(shù)據(jù)庫(kù)中;
用 phpMyAdmin 和 mysqldump 導(dǎo)出的數(shù)據(jù)總是亂碼。

總之你用 phpMyAdmin 和 mysqldump 導(dǎo)出的數(shù)據(jù)有亂碼時(shí),就試試看這個(gè)腳本吧。

使用很簡(jiǎn)單:

php export_db.php 數(shù)據(jù)庫(kù)名 [-h 主機(jī)名] [-c 字符集] [-f 輸出文件名] [-u 用戶名] [-p]
數(shù)據(jù)庫(kù)名是必須提供的,其他參數(shù)如果沒(méi)有提供則使用下面的默認(rèn)值:
默認(rèn)主機(jī)名 : localhost
默認(rèn)字符集 : utf8
默認(rèn)用戶名 : root
默認(rèn)密碼 : (無(wú))
默認(rèn)輸出文件 : 數(shù)據(jù)庫(kù)名.sql

這個(gè)腳本的導(dǎo)出結(jié)果就是一個(gè) .sql 文件,只有 insert 語(yǔ)句。
所以數(shù)據(jù)結(jié)構(gòu)需要單獨(dú)導(dǎo)出,不過(guò)這個(gè)就不存在字符集問(wèn)題了。

源代碼:

[php]


<?php
if (!function_exists('mysql_connect')) {
if (DIRECTORY_SEPARATOR == '/') {
dl('php_mysql.so');
} else {
dl('php_mysql.dll');
}
}
$database = null;
if (isset($argv[1])) {
$database = $argv[1];
} else {
display_help();
exit;
}
$optional_args = array(
'-h' => 'hostname',
'-c' => 'charset',
'-f' => 'filename',
'-u' => 'username'
);
$options = array(
'hostname' => 'localhost',
'charset' => 'utf8',
'filename' => '%s.sql',
'username' => 'root',
);
$input_password = false;
for ($i = 2; $i < $argc; $i++) {
$arg = $argv[$i];
if ($arg == '-p') {
$input_password = true;
continue;
}
if (isset($optional_args[$arg])) {
$value_name = $optional_args[$arg];
if (isset($argv[$i + 1])) {
$options[$value_name] = $argv[$i + 1];
$i++;
}
}
}
if ($input_password) {
echo "password: ";
fscanf(STDIN, '%s', $password);
$options['password'] = $password;
echo "\n";
} else {
$options['password'] = '';
}
if ($database == null) {
display_help();
exit;
}
mysql_connect($options['hostname'], $options['username'], $options['password']);
mysql_select_db($database);
mysql_query("SET NAMES '{$options['charset']}'");
// 設(shè)置要導(dǎo)出的表
$tables = list_tables($database);
$filename = sprintf($options['filename'], $database);
$fp = fopen($filename, 'w');
foreach ($tables as $table) {
dump_table($table, $fp);
}
fclose($fp);
mysql_close();
echo "done.\n";
exit;
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;
}
function dump_table($table, $fp = null)
{
$need_close = false;
if (is_null($fp)) {
$fp = fopen($table . '.sql', 'w');
$need_close = true;
}
fwrite($fp, "-- \n-- {$table}\n-- \n");
$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);
}
fwrite($fp, "\n\n");
}
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) . ");\n";
return $sql;
}
function display_help()
{
echo <<<EOT
syntax:
php export_db.php database [-h hostname] [-c charset] [-f filename] [-u username] [-p]
defualt hostname : localhost
default charset : utf8
default username : root
default password : (none)
default filename : [database].sql
EOT;
}
?>

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多