I created a WebService in php for get data from mySQL DB and returns a JSON.
So, I need that, whenever have null value from my DB, this value is changed for white (= " ").
I did this way, but without success (look the last foreach):
<?php
header("Content-Type: application/json; charset=utf-8;");
include('connectdb.php');
$something = $_GET['cod'];
$sqlcode = mysql_query("Select descricao, cliente, local from terminal_cartao Where descricao='$something'");
$sqlcode2 = mysql_query("Select descricao, cliente, local from terminal_cartao");
$jsonObj= array();
if($something == 'all')
{
while($result=mysql_fetch_object($sqlcode2))
{
$jsonObj[] = $result;
}
}
else{
while($result=mysql_fetch_object($sqlcode))
{
$jsonObj[] = $result;
}
}
foreach ($jsonObj as $key => $value) {
if ($value === null) {
$jsonObj[$key] = "";
}
}
$final_res =json_encode($jsonObj);
echo $final_res;
exit;
var_dump($value)inside foreach and verify what it returns carefully. Outside the question please refactor your code.