php 数据库内容导出打印函数代码

/**
		 * 数据库内容导出
		 * @param $host         database host
		 * @param $user         username
		 * @param $pwd          password
		 * @param $db           database name
		 * @param $table        only dump one table
		 * @param $filename     custom file to write output content
		 */
		public static function dbDump($host, $user, $pwd, $db, $table = null, $filename = null) {
			$mysqlconlink = mysql_connect($host, $user, $pwd, true);
			if (!$mysqlconlink)
				echo sprintf(‘No MySQL connection: %s‘,mysql_error())."<br/>";
			mysql_set_charset( ‘utf8‘, $mysqlconlink );
			$mysqldblink = mysql_select_db($db,$mysqlconlink);
			if (!$mysqldblink)
				echo sprintf(‘No MySQL connection to database: %s‘,mysql_error())."<br/>";
			$tabelstobackup = array();
			$result = mysql_query("SHOW TABLES FROM `$db`");
			if (!$result)
				echo sprintf(‘Database error %1$s for query %2$s‘, mysql_error(), "SHOW TABLE STATUS FROM `$db`;")."<br/>";
			while ($data = mysql_fetch_row($result)) {
				if(empty($table)) {
					$tabelstobackup[] = $data[0];
				} else if(strtolower($data[0]) == strtolower($table)){  //only dump one table
					$tabelstobackup[] = $data[0];
					break;
				}
			}
			if (count($tabelstobackup)>0) {
				$result=mysql_query("SHOW TABLE STATUS FROM `$db`");
				if (!$result)
					echo sprintf(‘Database error %1$s for query %2$s‘, mysql_error(), "SHOW TABLE STATUS FROM `$db`;")."<br/>";
				while ($data = mysql_fetch_assoc($result)) {
					$status[$data[‘Name‘]]=$data;
				}
				if(!isset($filename)) {
					$date = date(‘YmdHis‘);
					$filename = "{$db}.{$date}.sql";
				}
				if ($file = fopen($filename, ‘wb‘)) {
					fwrite($file, "-- ---------------------------------------------------------\n");
					fwrite($file, "-- Database Name: $db\n");
					if(empty($table)) {    //if not only dump single table, dump database create sql
						self::_db_dump_create_database($db, $file);
					}
					fwrite($file, "-- ---------------------------------------------------------\n\n");
					fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n");
					fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n");
					fwrite($file, "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n");
					fwrite($file, "/*!40101 SET NAMES ‘".mysql_client_encoding()."‘ */;\n");
					fwrite($file, "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n");
					fwrite($file, "/*!40103 SET TIME_ZONE=‘".mysql_result(mysql_query("SELECT @@time_zone"),0)."‘ */;\n");
					fwrite($file, "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n");
					fwrite($file, "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n");
					fwrite($file, "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */;\n");
					fwrite($file, "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n");
					foreach($tabelstobackup as $table) {
//						echo sprintf(‘Dump database table "%s"‘,$table)."<br/>";
						self::need_free_memory(($status[$table][‘Data_length‘]+$status[$table][‘Index_length‘])*3);
						self::_db_dump_table($table,$status[$table],$file);
					}
					fwrite($file, "\n");
					fwrite($file, "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n");
					fwrite($file, "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n");
					fwrite($file, "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n");
					fwrite($file, "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n");
					fwrite($file, "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n");
					fwrite($file, "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n");
					fwrite($file, "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n");
					fwrite($file, "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n");
					fclose($file);
//					echo ‘Database dump done!‘."<br/>";
					return $filename;
				} else {
					echo ‘Can not create database dump!‘."<br/>";
				}
			} else {
				echo ‘No tables to dump‘."<br/>";
			}
		}

  解决了小程序开发导出数据库常见问题。上面的是 TP3.2 php版本5.6的代码,大家可以直接复制使用,调用方法就可以了。

php 数据库内容导出打印函数代码

上一篇:adb连接mumu模拟器,清除密码


下一篇:php的cURL库介绍