PHP Classes
elePHPant
Icontem

File: db

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Mark Williamson  >  mysqlLikeDisplay  >  db  >  Download  
File: db
Role: Example script
Content type: text/plain
Description: Query mysql database from linux command line and see results in mysql style table
Class: mysqlLikeDisplay
Render array structs in mysql table style or html
Author: By
Last change:
Date: 12 years ago
Size: 2,135 bytes
 

Contents

Class file image Download
#!/usr/local/bin/php -q
<?php
//sample calls to this function.......

$helptext = "
    --------------------------------
            /|/| /||)|/ fate_amendable_to_change@yahoo.com
     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";
}

?>