I am doing a web service in php to encode data to json. My php code is:
function getProgramDay($day)
{
global $DDBB_SERVER, $DDBB_USER, $DDBB_PASSWORD, $DDBB;
$sql = "SELECT program.id, programa.name FROM `mybd` WHERE program.day = '" . $day . "'";
$con = mysqli_connect($DDBB_SERVER, $DDBB_USER, $DDBB_PASSWORD, $DDBB);
if (!$con) {
die('Error de Conexión (' . mysqli_connect_errno() . '): ' . mysqli_connect_error());
}
if (!mysqli_set_charset($con, "utf8")) {
die("Error loading character set utf8:" . mysqli_error($con));
}
$result = mysqli_query($con, $sql);
$res = array();
// Prepare data
while ($row = mysqli_fetch_assoc($result)) {
$res[] = $row;
}
// Free resultset
mysqli_free_result($result);
// Close connection
mysqli_close($con);
// Return data
return $res;
}
And the result of this function in json is:
[{"id":"1","nombre":"Hello"}]
I would like that the result will be:
{"results":[{"id":"1","nombre":"Hello"}]}
How can I do this? I have tried but not works:
return "{'results':" .$res . "}";
Thank you so much :-)
$res['results'][] = $row;