<?php 
error_reporting(0); 

/** 
 * 
 * Use this class to open a MySQL shell 
 * @author Dev Sharma (xxprogrammerxx56@gmail.com) 
 * @copyright You can do anything you want as long as you do not claim you made this script. 
 * @category Database Access 
 * 
 */ 

class Shell 
{ 
    /** 
     * 
     * For connecting to your MySQL server 
     * @var String 
     */ 
    private $host; 
    /** 
     * 
     * Username that will be required for MySQL Authentication 
     * @var String 
     */ 
    private $username; 
    /** 
     * 
     * Password that will be required for MySQL Authentication 
     * @var String 
     */ 
    private $password; 
    /** 
     * 
     * Database name so that you can execute queries on that specific database 
     * @var String 
     */ 
    private $dbname; 
    /** 
     * 
     * Database port so that you can connect to the mysql server 
     * @var Int 
     */ 
    private $port; 
    /** 
     * 
     * This variable will handle the mysqli class, in other words handle connection. 
     * @var ObjectConnection 
     */ 
    private $db; 
    
    /** 
     * @param string $host 
     * @param string $username 
     * @param string $password 
     * @param string $db 
     * @param int $port 
    */ 

    function __construct($host, $username, $password, $db, $port) 
    { 
        $this->host = $host; 
        $this->username = $username; 
        $this->password = $password; 
        $this->dbname = $db; 
        $this->port = $port; 

        $this->db = new mysqli($this->host, $this->username, $this->$password, $this->dbname, $this->port); 
        $this->db->set_charset("utf8"); 

        if ($this->db->connect_errno) die('Error: Unable to connect to Database.'); 
    } 
    
    /** 
     * The main function which handles your MySQL query using shell 
     * @uses Private use 
    */ 

    public function shell() 
    { 
        $response = array(); 
        $exit = false; 

        echo "\n\r\n\r" . 'The simple MYSQL client shell' . "\n\r\n\r"; 

        while (!$exit) { 
            echo "\r\n"; 

            echo "Enter the query you want to execute: "; 
            $query = trim(fgets(STDIN)); 

            if ($query == "exit") $exit = true; 

            $results = $this->db->query($query); 

            if (!$results) { 
                $response = array($this->db->error); 
            } else { 
                while ($row = $results->fetch_array(MYSQLI_ASSOC)) { 
                    $response[] = $row; 
                } 
            } 
            echo "\r\n"; 
            print_r($response); 
        } 

        echo 'Have a nice day!'; 
    } 
} 

?>