<?php
class Database
{
    private $servername = "127.0.0.1";
    /*private $username = "root";
    private $password = "gsfo115485";
    private $dbname = "trad";*/

    private $username = "roc_precises";
    private $password = "@gsfo115485";
    private $dbname = "roc_precises";


    public $conn;

    public function ExecuteQuery($query)
    {
        //echo $query;
        $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
        // Check connection
        if ($conn->connect_error) {
            //die("Connection failed, please, check with support!: " . $conn->connect_error);
            die("Connection failed, please, check with support!: ");
        }
        //echo $query;
        if ($conn->query($query) === TRUE) {
            $conn->close();
            return true;
        } else {
            //echo "Error: " . $query . "<br>" . $conn->error;
            $conn->close();
            return false;
        }
    }

    public function ExecuteQueryReturnId($query)
    {
        //echo $query;
        $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
        // Check connection
        if ($conn->connect_error) {
            //die("Connection failed: " . $conn->connect_error);
            die("Connection failed, please, check with support!: ");
        }
        if ($conn->query($query) === TRUE) {
            return mysqli_insert_id($conn);
        } else {
            //echo "Error: " . $query . "<br>" . $conn->error;
            $conn->close();
            return 0;
        }
    }

    public function ExecuteReader($query)
    {
        $res_return = array();
        $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
           // die("Connection failed, please, check with support!: ");
        }
        //echo $query;
        $res = $conn->query($query);
        while ($row = $res->fetch_array())
        {
            $res_return[] = $row;
        }
        $conn->close();
        return $res_return;
    }

    public function ExecuteUniqueReader($query)
    {
        $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
        // Check connection
        if ($conn->connect_error) {
            //die("Connection failed: " . $conn->connect_error);
            die("Connection failed, please, check with support!: ");
        }
        $returnArray = $conn->query($query);
        $conn->close();
        //echo $query;
        try{
            if(!$returnArray){
                return array();
            }else{
                return $returnArray->fetch_array(MYSQLI_ASSOC);
            }
        }
        catch(Exception $e){
            return null;
        }

    }

}


?>