0

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;
1
  • var_dump($value) inside foreach and verify what it returns carefully. Outside the question please refactor your code. Commented Nov 4, 2014 at 10:26

2 Answers 2

1
foreach ($jsonObj as $key => $value) {
  $value = get_object_vars($value);
  foreach($value as $key2 => $value2) {
    if ($value2 === null) {
      $jsonObj[$key]->$key2 = ""; 
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The code bellow works fine for me now:

foreach ($jsonObj as $k1 => $row) {
foreach ($row as $k2 => $value) {
    if ($value === null) {
        $jsonObj[$k1]->$k2 = "";
    }
}
} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.