Skip to content

Instantly share code, notes, and snippets.

@Sequoia
Created January 24, 2012 17:50
Show Gist options
  • Save Sequoia/1671491 to your computer and use it in GitHub Desktop.
Save Sequoia/1671491 to your computer and use it in GitHub Desktop.
A simple php+bash script to output the db schema in the nicely formatted mysql c client way (w/o reproducing the formatting work)
<?php
//I'd add a shebang line but my php is /usr/local/zend/... and yours probably isn't
//if you want to run it that way, make sure to offset the argv keys by -1
//explain usage
if( !isset( $argv[1] ) ){
echo "Describes all tables in the nice mysql c client way\n";
echo "Usage: php " . basename(__FILE__) . " [username] [password] [database]";
die("\n");
}
$user = $argv[1];
$password = $argv[2];
$db = $argv[3];
//init DB connection
$mysqli = new mysqli('localhost', $user, $password, $db);
//die on conn error
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error . "\n");
}
//buffer output
ob_start();
//get table names
$tables = $mysqli->query('SHOW TABLES;');
while($row = $tables->fetch_array(MYSQLI_NUM)){
passthru("mysql -v -v -v -u$user -p$password -e'desc $row[0]' $db | sed '/Bye/d; s/desc //; /^[0-9]\+ rows/d'");
}
ob_flush();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment