<?php
//DB config
//include 'config.php';

class conexion_sec {

    var $db_host;
    var $db_user;
    var $db_pass;
    var $db_name;

    function __construct($DB_HOST_, $DB_USER_, $DB_PASS_, $DB_NAME_)
    {
        $db_host = $DB_HOST_;
        $db_user = $DB_USER_;
        $db_pass = $DB_PASS_;
        $db_name = $DB_NAME_;

        //Make a connetion with the db
        $connection = mysqli_connect($db_host,$db_user,$db_pass,$db_name);

        if(!$connection)
        {
            echo "Error en la conexion a la BD"."<br>";
            echo mysqli_connect_error();
        }
    }
    //insert querys
    function insert_query($query, $db_host,$db_user,$db_pass,$db_name)
    {
        //room for inprovement
        $connection = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
        mysqli_set_charset($connection, "utf8");
        $result = mysqli_query($connection, $query);
        return $result;
    }

    function selectQuery($query, $db_host,$db_user,$db_pass,$db_name)
    {
        //room for inprovement
        $connection = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
        mysqli_set_charset($connection, "utf8");
        $result = mysqli_query($connection, $query);
        return $result;
    }

    function JsonQuery($query, $db_host,$db_user,$db_pass,$db_name)
    {
        //room for inprovement
        $connection = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
        $prequery = "SET lc_time_names = 'es_ES'"; //Put the date information in spanish
        mysqli_query($connection,$prequery); //execute the query in spanish
        mysqli_set_charset($connection, "utf8");
        $resultado = mysqli_query($connection, $query);

        // Si hay un error en el SQL
        if (!$resultado ) {
        echo "class.Database.class: error ". $mysqli->error;
        }

        if (mysqli_num_rows($resultado)==0)
        {
            return "null";
        }
        else {
            if ($resultado)
            {
            $i = 0;
                    while($row = $resultado->fetch_assoc()){
                        $registros[$i]= $row;
                        $i++;
                    };
                    return json_encode($registros);
            }
        }
    }

    function disconnect($db_host,$db_user,$db_pass,$db_name)
    {
        $connection = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
        mysqli_close($connection);
    }

}

?>