Connect to mysql and show SELECT data

Assuming you prepared .env file like

DBHOST=127.0.0.1
DBUSER=someusername
DBPASSWORD=somepassword

for connection to mysql server

JavaScript

class Rdb {
	constructor(opt={}) {
    this.mysql = require('mysql');
    require('dotenv').config();
    this.mdbh = this.mysql.createConnection({
      host: process.env.DBHOST,
      port: 3306,
      user: process.env.DBUSER,
      password: process.env.DBPASSWORD,
      database: 'mysql'
    });
  }

	dbconnect() {
    this.mdbh.connect(function (err) {
      if (err) {
          console.error('error connecting: ' + err.stack);
          return;
      }
    });
  }

	dbclose() {
    this.mdbh.end();
  }

  showSytemUsers() {
    const sql = "SELECT host,user,Select_priv FROM mysql.user";
    this.mdbh.query(sql, function (err, rows, fields) {
      if (err) { 
        console.log('err: ' + err);
      }
      for(let row of rows) {
        console.log([row.host, row.user, row.Select_priv].join("\t"));
      }
    });
  }
};
module.exports = Rdb;

if(!module.parent) {
	let rdb = new Rdb();
	rdb.dbconnect();
  rdb.showSytemUsers();
  rdb.dbclose();
}

Perl

package Rdb;
use DBI;
use Dotenv;

sub new() {
  my $self = {};
  Dotenv->load();
  return bless $self;
}

sub dbconnect(){
  my $self = shift;
  my $database = "mysql";
  $self->{'mdbh'} = DBI->connect("DBI:mysql:".$database.":".$ENV{'DBHOST'}, $ENV{'DBUSER'}, $ENV{'DBPASSWORD'}) or die "Failed to connect to mysqldb.";
}

sub dbclose(){
  my $self = shift;
  $self->{'mdbh'}->disconnect();
}

sub showSystemUsers(){
  my $self = shift;
  my $sql = 'SELECT host,user,Select_priv FROM mysql.user';
  my $sth = $self->{'mdbh'}->prepare($sql);
  $sth->execute();
  while(my $row_ref = $sth->fetchrow_hashref) {
    print(join("\t", $row_ref->{'host'}, $row_ref->{'user'}, $row_ref->{'Select_priv'})."\n");
  }
  $sth->finish();
}

if ($0 eq __FILE__) {
  my $rdb = new Rdb();
  $rdb->dbconnect();
  $rdb->showSystemUsers();
  $rdb->dbclose();
}
else{
  1;
}

PHP

<?php
require_once __DIR__ . '/vendor/autoload.php';

class Rdb {
  private $mdbh;
  public function __construct($opt=[]) {
    $dotenv = Dotenv\Dotenv::create(__DIR__);
    $dotenv->load();
    print("Loaded");
  }

  function dbconnect() {
    try {
      print implode("\t", [getenv('DBHOST'), getenv('DBUSER'), getenv('DBPASSWORD')]);
      $this->mdbh = new PDO('mysql:dbname=mysql;host='.getenv('DBHOST'), getenv('DBUSER'), getenv('DBPASSWORD'));
      $this->mdbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
      die("Failed to connect: ".$e->getMessage());
    }
  }

  function dbclose() {
    $this->mdbh = null;
  }

  function showSystemUsers() {
    $sth = $this->mdbh->prepare("SELECT host,user,Select_priv FROM mysql.user");
    $sth->execute();
    while($row = $sth->fetch(PDO::FETCH_ASSOC)){
      print(implode("\t", [$row['host'], $row['user'], $row['Select_priv']])."\n");
    }
  }
}

if ( !isset(debug_backtrace()[0]) ) {
  $rdb = new Rdb();
  $rdb->dbconnect();
  $rdb->showSystemUsers();
  $rdb->dbclose();
}

Python

from firstclass_dotenv import Dotenv
import mysql.connector
import os

class Rdb():
  def __init__(self, opt=[]):
    dotenv = Dotenv()
    dotenv.load()

  def dbconnect(self):
    self.mdbh = mysql.connector.connect(host=os.environ['DBHOST'], user=os.environ['DBUSER'], passwd=os.environ['DBPASSWORD'])

  def showSystemUsers(self):
    sql = "SELECT host,user,Select_priv FROM mysql.user"
    cur = self.mdbh.cursor()
    cur.execute(sql)
    for [host, user, selectPriv] in cur:
      print("\t".join([host.decode(), user.decode(), selectPriv]))
    cur.close()

  def dbclose(self):
    self.mdbh.close()

if __name__ == "__main__":
  rdb = Rdb()
  rdb.dbconnect()
  rdb.showSystemUsers()
  rdb.dbclose()

Ruby

require 'dotenv'
require 'mysql2'

class Rdb
  def initialize(opt=[])
    Dotenv.load(
      File.join(File.dirname(File.expand_path(__FILE__)), '.env')
    )
  end

  def dbconnect
    @mdbh = Mysql2::Client.new(:host => ENV["DBHOST"], :username => ENV["DBUSER"], :password => ENV["DBPASSWORD"])
  end

  def showSystemUsers
    results = @mdbh.query("SELECT host,user,Select_priv FROM mysql.user")
    results.each do |row|
      print([row["host"], row['user'], row['Select_priv']].join("\t") + "\n")
    end
  end

  def dbclose
    @mdbh.close()
  end
end

if $0 == __FILE__
  rdb = Rdb.new()
  rdb.dbconnect()
  rdb.showSystemUsers()
  rdb.dbclose()
end