#!/usr/bin/php
<?php
// do not run on the web
if(isset($_SERVER['SERVER_NAME'])) {
  exit;
}

// the special array $argv contain the script name
// followed by the arguments. Define arguments

// the table to get the random row from
if(isset($argv[1])) {
  $table=$argv[1];
}

// the table column
if(isset($argv[2])) {
 $column=$argv[2];
}

// the file
if(isset($argv[3])) {
 $file=$argv[3];
}

// the first argument is the usermane
if(isset($argv[4])) {
 $username=$argv[4];
}

// the second argument is the password
if(isset($argv[5])) {
   $password=$argv[5];
}

if(! isset($password)) {
  print "You need five argument: table, column, file, username and password\n";
  exit;
 }

// the file path is supposed to be relative to the user's
// public_html directory
$home_dir='/home/'.$username;

// check that the homedirectory exists
if(! is_dir($home_dir)) {
  print "No such user: $username,\n";
  print "because no such directory: $home_dir.\n";
  exit;
 }

// the public_html directory
$public_dir=$home_dir.'/public_html';

// the name of the output file 
$out_file_name="$public_dir/$file";

// try to open file
$out_file = fopen($out_file_name, "w");
if(! $out_file) {
  print "Could not open file for writing: $out_file_name\n";
  exit;
 }

//now proceed to the databaes
$link=@mysqli_connect('localhost',$username,$password);
if (!$link) {
  print "mysql error: ";
  print mysqli_connect_error();
  print "\n";
  exit;
 }

// the datebase is the same as the user name
@mysqli_select_db($link,$username) ;
report_error($link);

//  select all records from the table
$query='SELECT * from '.$table.';';
$result=@mysqli_query($link,$query);
report_error($link);


// form a keys numeric array with the first 
while($columns=mysqli_fetch_array($result)) {
  if(! isset($columns[$column])) {
    print "Column \"$column\" in not defined in table $table.\n";
    print "The password given was $password\n";
    exit;
  }
  // the array of the columns
  $column_to_random[]=$columns[$column];
 }

// from http://php.net/function.array-rand.php
srand((float) microtime() * 10000000);
$random_column=array_rand($column_to_random, 1);

// the output has the timestamp and the value of the column
$out_string =time()." $random_column\n";

// write out_string to file
fwrite($out_file,$out_string);

// close out_file
fclose($out_file);

// The End

function report_error ($link) {
  $error=mysqli_error($link);
  if($error) {
    print "mySQL error: $error\n";
    exit;
  }
}


?>

Valid XHTML 1.0!