#!/usr/local/bin/php -q
 
<?php
 
//sample calls to this function.......
 
 
$helptext = "
 
    --------------------------------
 
            /|/| /||)|/ [email protected]
 
     madeby/   |/-||\|\
 
    --------------------------------
 
 
 you must specify a database to connect to - even if this is just a space
 
 for commands without needing this info - eg: processlist, show databases, etc
 
 
### sample usage ####
 
 
//show current process list
 
db \"show processlist\" \" \"
 
 
//describe the services table of userdata database
 
db \"desc services\" \"userdata\"
 
 
// get 10 username/service_id's from the services table of userdata
 
db \"select username, service_id from services limit 10\", \"userdata\"
 
 
 
#### intergrating with bash ######
 
 
// show all the tables for databases userdata, financial and products.
 
for i in `db \"show databases\" \" \"; do db \"show tables\" \"$i\"; done
 
 
// update servies table - set blah=\"blah\" for each username in a text file called biglistofusers.
 
echo for i in `cat biglistofusers`; do db \"update services set blah=\'blah\' where username = \'$i\'; done
 
\"
 
";
 
 
 
$DBHOST = "localhost";
 
$DBUSER = "";
 
$DBPASS = "";
 
 
 
$sql = $argv[1];
 
$db = $argv[2];
 
$no_output = $argv[3]; //if true - dont draw mysql table round results
 
 
if(empty($sql) || empty($db)){
 
    echo "Error! No sql or database name passed to function!\n";
 
    echo "\nUsage: db 'SELECT * FROM BLA' 'database_name' \n";
 
    echo $helptext;
 
    die();
 
}
 
 
$connection = mysql_pconnect("$DBHOST", "$DBUSER", "$DBPASS") or die(mysql_error());
 
$foo = mysql_select_db($db);
 
$result = mysql_query($sql, $connection) or die(mysql_error()."\n");
 
 
while($rows = mysql_fetch_array($result, MYSQL_ASSOC)){
 
    $all[] = $rows;
 
}
 
 
if($no_output){
 
    foreach($all as $xkey=>$xval){
 
        foreach($xval as $zkey=>$zval){
 
            echo $zval."\n";
 
        }
 
    }
 
}
 
else{
 
        include("./mysqllikedisplay.inc");
 
          //uses mysqllikedisplay class to render mysql style table
 
        $t = new display($all);
 
        $t->make_layout();
 
        echo "\n\n";
 
}
 
 
?>
 
 
 |