1

I have two sql files(t1.sql and t2.sql). Two sql files are different versions and two files contains create queries. I want to compare two sql files query to check the query structures are changed or not.

For example:-

t1.sql file:
CREATE TABLE `static_ids` (
  `access_id` int(11) unsigned NOT NULL DEFAULT '0',
  `group_id` int(11) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`access_id`,`group_id`)
) ENGINE=MyISAM;

t2.sql file:
CREATE TABLE `static_ids` (
  `access_id` int(11) unsigned NOT NULL DEFAULT '0',
  `group_id` int(11) unsigned NOT NULL DEFAULT '0',
  `group_name` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`access_id`,`group_id`)
) ENGINE=MyISAM;

In this example, create query for the table static_ids structure is different.

Kindly give any idea to compare two sql file queries through PHP. Thanks in advance.

raj
  • 261
  • 3
  • 14
  • Possible duplicate of [Can I use file\_get\_contents() to compare two files?](http://stackoverflow.com/questions/3060125/can-i-use-file-get-contents-to-compare-two-files) – check Oct 28 '15 at 07:21
  • If any additional white space exists means it wouldnt work. I wanna to check each sql file queries structure exactly. – raj Oct 28 '15 at 07:26

1 Answers1

1

To reliably check if the structure is the same using only PHP, you would basically have to rewrite the MySQL parser. It would have to understand, for instance, that

id     int;

is equivalent to

`id` int(11) signed DEFAULT NULL;

The much easier solution is to just run the CREATE TABLE command on a blank MySQL database, then do DESCRIBE table_name to a get a list of all the columns that were created as. You can then run through the list of columns for the two tables in a PHPfor` loop and compare them.

Cuagau
  • 548
  • 3
  • 7