DataConstruct.php
<?php

/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/7/21
* Time: 14:26
* 列子
* $pdo=new DataConstruct();
* $tables=$pdo->getAllTableNames();
* $pdo->printLog($pdo->getAllTableConstruct($tables),"F:/logstr.php");
* $pdo->importSql('E:/UserTable.sql');
*/class DataConstruct
{ public $dataBaseType;
public $host;
public $username;
public $password;
public $port;
public $database;
public $conn; public function __construct($username = "root", $password = "", $database = "aui", $port = "3306", $host = "localhost", $dataBaseType = "mysql")
{
$this->dataBaseType = $dataBaseType;
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->port = $port;
$this->database = $database;
$dsn = "$dataBaseType:dbname=$database;host=$host";
try {
$this->conn = new \PDO($dsn, $this->username, $this->password); } catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
} } /**
* @desc 获取所有的额表明
* @return array
*/
public function getAllTableNames()
{
$tableNames = array();
$sql = "show tables";
$cursor = $this->conn->query($sql);
foreach ($cursor as $row) {
$tableNames[] = $row[0];
}
return $tableNames; } /**
* @desc 获取某个表的表结构
* @param $tablename
* @return array
*/
public function getTableConstruct($tablename)
{
$table = array();
$sql = "select COLUMN_NAME,COLUMN_TYPE,COLUMN_DEFAULT,COLUMN_KEY,COLUMN_COMMENT from information_schema.`COLUMNS` where TABLE_NAME='" . $tablename . "' and TABLE_SCHEMA='" . $this->database . "'";
$cursor = $this->conn->query($sql, \PDO::FETCH_ASSOC);
foreach ($cursor as $row) {
$table[$row['COLUMN_NAME']] = $row;
}
return $table;
} /**
* @desc 获取所有表的表结构
* @param array $tableNames
* @return array
*/
public function getAllTableConstruct($tableNames = array())
{
$AllTableConstruct = array();
if (is_array($tableNames)) {
foreach ($tableNames as $tableName) {
$AllTableConstruct[$tableName] = $this->getTableConstruct($tableName);
}
}
return $AllTableConstruct;
} /**
* @desc 打印表结构到指定的文件中
* @param $tableConstruct
* @param $logpath
*/
public function printLog($tableConstruct, $logpath)
{ foreach ($tableConstruct as $key => $table) {
file_put_contents($logpath, $key . PHP_EOL, FILE_APPEND);
file_put_contents($logpath, "COLUMN_NAME COLUMN_TYPE COLUMN_DEFAULT COLUMN_KEY COLUMN_COMMENT" . PHP_EOL, FILE_APPEND);
foreach ($table as $column) {
file_put_contents($logpath, $column['COLUMN_NAME'] . "\t" . $column['COLUMN_TYPE'] . "\t" . $column['COLUMN_DEFAULT'] . "\t" . $column['COLUMN_KEY'] . "\t" . $column['COLUMN_COMMENT'] . PHP_EOL, FILE_APPEND);
}
file_put_contents($logpath, PHP_EOL, FILE_APPEND);
}
} /**
* @desc 执行sql文件 (不能超温php.ini中配置的内存大小,否则会报内存溢出错误)
* @param $filepath
*/
public function importSql($filepath)
{
$_sql = file_get_contents($filepath);
$_arr = explode(';', $_sql);
$this->conn->query('set names utf8;');
foreach ($_arr as $_value) {
try {
$this->conn->query($_value . ';');
} catch (\PDOException $e) {
var_dump($this->conn->errorInfo());
} }
}
}
new.php   
<?php
include_once("DataConstruct.php");
//---------------------------------------比较两个不同的数据库之间的表结构-start-------------------------------
$pdo=new DataConstruct();
$oldtables=$pdo->getAllTableNames();
$pdonew=new DataConstruct("root",'','test');
$newtables=$pdonew->getAllTableNames();
foreach($newtables as $key => $val){
if(in_array($val,$oldtables)){
$oldTableCu=$pdo->getTableConstruct($val);
$newTableCU=$pdonew->getTableConstruct($val);
if(!empty(array_diff($oldTableCu,$newTableCU))){//表示不相同,没有变化
echo "---diff table---".$val;
}
}else{
echo "---new table---".$val;
}
}
//---------------------------------------比较两个不同的数据库之间的表结构--end------------------------------
?>

最新文章

  1. Eclipse使用Git教程
  2. ::before/:before和::after/:after的使用
  3. Hibernate总结
  4. 001. 使用ssh连接不上centos 6.5的解决方法及其解决中文乱码
  5. 到目前为止,Linux下最完整的Samba服务器配置攻略
  6. Altium 9中正确的差分对走线方法
  7. Java 循环和函数(方法)
  8. numpy学习总结(重点讲解索引方式)
  9. ARM的编程模式
  10. Oracle JDK 1.8 openJDK11 定制化JDK
  11. Java Web 项目发布到Tomcat中三种部署方法
  12. 批量镜像locator(比如表情模板)
  13. php项目核心业务(增、删、改、查)(第三篇)
  14. [C++]四分树(Quadtrees)
  15. a标签强制不换行
  16. Window对象属性
  17. php中文件操作常用函数有哪些
  18. shell 脚本传参
  19. JFinal Web开发学习(一)开启HelloWorld
  20. Visual Studio(VS)C++单元测试

热门文章

  1. Java并发编程(七)线程封闭
  2. 15:开发Rsync服务启动脚本案例
  3. 【转】 Monkey测试1——Monkey的使用
  4. jmeter操作myql数据库
  5. SharePoint服务器端对象模型 之 访问网站和列表数据(Part 1)
  6. jquery lazyload延迟加载技术的实现原理分析_jquery
  7. How to make asynchronous HTTP requests in PHP 4种PHP异步执行的常用方式
  8. 如何对你的Python代码进行基准测试
  9. PAT 1072. 开学寄语(20) JAVA
  10. zookeeper单机伪集群配置