PHP技巧:用PHP導(dǎo)出MySQL數(shù)據(jù)庫內(nèi)容為.sql文件_PHP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:PHP生成靜態(tài)頁面完整教程我們先回顧一些基本的概念。 一、PHP腳本與動態(tài)頁面。 PHP腳本是一種服務(wù)器端腳本程序,可通過嵌入等方法與HTML文件混合,也可以類,函數(shù)封裝等形式,以模板的方式對用戶請求進(jìn)行處理。無論以何種方式,它的基本原理是這樣的。由客戶端提出請求,請求某一頁
通常我們都用 phpMyAdmin 來導(dǎo)出,不過如果你的數(shù)據(jù)庫如果存在下列問題,那么 phpMyAdmin 也無能為力。
- 數(shù)據(jù)庫的字符集與應(yīng)用程序的字符集不一致;
- 應(yīng)用程序用錯誤的編碼將數(shù)據(jù)保存到了數(shù)據(jù)庫中;
- 用 phpMyAdmin 和 mysqldump 導(dǎo)出的數(shù)據(jù)總是亂碼。
總之你用 phpMyAdmin 和 mysqldump 導(dǎo)出的數(shù)據(jù)有亂碼時,就試試看這個腳本吧。
使用很簡單:
php export_db.php 數(shù)據(jù)庫名 [-h 主機名] [-c 字符集] [-f 輸出文件名] [-u 用戶名] [-p]
數(shù)據(jù)庫名是必須提供的,其他參數(shù)如果沒有提供則使用下面的默認(rèn)值:
默認(rèn)主機名 : localhost
默認(rèn)字符集 : utf8
默認(rèn)用戶名 : root
默認(rèn)密碼 : (無)
默認(rèn)輸出文件 : 數(shù)據(jù)庫名.sql
這個腳本的導(dǎo)出結(jié)果就是一個 .sql 文件,只有 insert 語句。
所以數(shù)據(jù)結(jié)構(gòu)需要單獨導(dǎo)出,不過這個就不存在字符集問題了。
源代碼:
- 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 <<
- 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;
- }
- ?>
下載:export_db.rar
來源:網(wǎng)絡(luò)
分享:PHP安全編程之加密功能數(shù)據(jù)加密在我們生活中的地位已經(jīng)越來越重要了,尤其是考慮到在網(wǎng)絡(luò)上發(fā)生的大量交易和傳輸?shù)拇罅繑?shù)據(jù)。如果對于采用安全措施有興趣的話,也一定會有興趣了解PHP提供的一系列安全功能。在本篇文章中,我們將介紹這些功能,提供一些基本的用法,以便你能夠為自
相關(guān)PHP教程:
- 相關(guān)鏈接:
復(fù)制本頁鏈接| 搜索PHP技巧:用PHP導(dǎo)出MySQL數(shù)據(jù)庫內(nèi)容為.sql文件
- 教程說明:
PHP教程-PHP技巧:用PHP導(dǎo)出MySQL數(shù)據(jù)庫內(nèi)容為.sql文件。