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

class conexion {

function __construct()
{
	//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)
{
	//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)
{
	//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)
{
	//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[]= $row;
				};
				return json_encode($registros);
		}
	}
}

function disconnect()
{
	$connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
	mysqli_close($connection);
}

}

?>
