Created
January 20, 2016 11:13
-
-
Save PEMapModder/e324734d01c5146d7905 to your computer and use it in GitHub Desktop.
Revisions
-
PEMapModder created this gist
Jan 20, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ #!/usr/bin/env php <?php if(!defined("STDIN")) define("STDIN", fopen("php://stdin", "R")); $host = "localhost"; $user = "root"; $password = ""; $schema = ""; $opts = getopt("h:u:p:s:"); if(isset($opts["h"])) $host = $opts["h"]; if(isset($opts["u"])) $user = $opts["u"]; if(isset($opts["p"])) $password = $opts["p"]; if(isset($opts["s"])) $schema = $opts["s"]; $db = @new mysqli($host, $user, $password, $schema); if(isset($db->conenct_error)) die($db->connect_error); echo "Connected!", PHP_EOL; while(true){ echo "> "; $line = rtrim(trim(fgets(STDIN)), ";"); if($line === "exit") break; $start = microtime(true); $result = $db->query($line); $end = microtime(true); if($result === false){ echo "Error: $db->error", PHP_EOL; }elseif($result instanceof mysqli_result){ $cols = []; $rows = 0; while(is_array($row = $result->fetch_assoc())){ foreach($row as $k => $v){ if(!isset($cols[$k])) $cols[$k] = [$k]; $cols[$k][] = $v; } $rows++; } $result->close(); if($rows === 0){ echo "Empty result set "; }else{ $paddings = []; foreach($cols as $k => $v){ $paddings[$k] = max(array_map("strlen", $v)); } $len = array_sum($paddings) + 1 + count($cols) * 3; for($i = 0; $i <= $rows; $i++){ if($i === 0) echo str_repeat("=", $len), PHP_EOL; foreach($cols as $k => $v){ echo "| " . str_pad($v[$i], $paddings[$k], " ", STR_PAD_BOTH) . " "; } echo "|", PHP_EOL; if($i === 0) echo str_repeat("=", $len), PHP_EOL; } echo str_repeat("=", $len), PHP_EOL; echo ($rows === 1 ? "1 row" : "$rows rows") . " in result set "; } }elseif(strtoupper(substr($line, 0, 6)) === "INSERT" and isset($db->insert_id)){ echo "Insert ID: $db->insert_id", PHP_EOL; }else{ echo "Query success "; } echo "(Query completed in " . round($end - $start, 4) . " second", $end - $start > 1 ? "s" : "", ")", PHP_EOL; } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ Why did I create this? === For some reason the MySQL client I downloaded with MySQL server does not work - it is stuck when connecting. Instead of spending time and trouble to find a fix for that, it is much simpler just to write MySQL own client script.