mysql - PHP - creating JSON but header doesn't appear -


i'm creating json string results of mysql query in php. reason php "header" function isn't appending when save results file sanity checks. below code:

        header("content-type: application/json");      if(mysql_num_rows($result)){             $dataresults = '{"data":[';             $first = true;             $row = mysql_fetch_assoc($result);              while($row = mysql_fetch_array($result, mysql_assoc)){                     if($first) {                             $first = false;                     } else {                             $dataresults = $dataresults . ',';                     }                     $dataresults = $dataresults . json_encode($row);             }             $dataresults = $dataresults . ']}';     } else {             $dataresults = '[]';     }      file_put_contents('/applications/mamp/htdocs/php/results.json', $dataresults); 

the output looks o.k., except missing "content-type: application/json". doing wrong?

why header() write file?

header() sets response header information using response hook in mod_php or whatever cgi equivalent if using cgi.

text files not contain meta information other encoding (if that).


Comments

Popular posts from this blog

jQuery clickable div with working mailto link inside -

java - Getting corefrences with Standard corenlp package -

WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl -