I dont know what’s wrong with this PHP code:
$sql = "CREATE TABLE test (
id mediumint(9) unsigned NOT NULL auto_increment,
filenames text NOT NULL,
meta longtext,
added_date datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (id)
)";
var_export(explode("\n",$sql));
The code above basically explode the string in $sql with newline char (‘\n’) and then output it using var_export. I have some PHP tests file to worked with the code but not all of them displaying what I hope it would be:
array ( 0 => 'CREATE TABLE test ( ',
1 => ' id mediumint(9) unsigned NOT NULL auto_increment, ',
2 => ' filenames text NOT NULL, ',
3 => ' meta longtext, ',
4 => ' added_date datetime NOT NULL default \'0000-00-00 00:00:00\', ',
5 => ' PRIMARY KEY (id) ', 6 => ') ;',
)
some diplaying this instead:
array ( 0 => 'CREATE TABLE test ( id mediumint(9) unsigned NOT NULL auto_increment, filenames text NOT NULL, meta longtext, added_date datetime NOT NULL default \'0000-00-00 00:00:00\', PRIMARY KEY (id) ) ;', )
notice the difference? the second one displaying it as there are no newline to exploding the string. I don’t get what’s happening here. Anyone knows something maybe?
You need to be flexible with what a
line feedlooks like. You can make a flexible explode:… or you can normalize line feeds to the format of your choice:
It’s also worth noting that MySQL offers several
SHOWstatements that allow you to fetch most useful piece of data from table definitions. Parsing theSHOW CREATE TABLEoutput is often not necessary:http://dev.mysql.com/doc/refman/5.1/en/show.html