I'm connecting to my database, but I do not want to show the result in the template of the page but to generate a file .json
.
The PHP code I use is:
<?php
$sql = "SELECT * FROM AttributeKeys WHERE atID = 1";
function connectDB(){
$server = "localhost";
$user = "user";
$pass = "pass";
$bd = "bd";
$conexion = mysqli_connect($server, $user, $pass,$bd);
return $conexion;
}
function disconnectDB($conexion){
$close = mysqli_close($conexion);
return $close;
}
function getArraySQL($sql){
//Creamos la conexión con la función anterior
$conexion = connectDB();
//generamos la consulta
mysqli_set_charset($conexion, "utf8");
if(!$result = mysqli_query($conexion, $sql)) die();
$rawdata = array();
//guardamos en un array multidimensional todos los datos de la consulta
$i=0;
while($row = mysqli_fetch_array($result))
{
$rawdata[$i] = $row;
$i++;
}
disconnectDB($conexion); //desconectamos la base de datos
return $rawdata; //devolvemos el array
}
$myArray = getArraySQL($sql);
echo json_encode($myArray);
?>
How could I solve this?