nesalexy
nesalexy

Reputation: 878

output json with database

Sorry for my English. I'm trying to output the data to a database format json. It seems to do everything right, but it is not true outputs. Here is my link which is obtained: http://ksupulse.tk/get_all.php if I did check the validity of the site http://jsonlint.com/ or http://jsonformatter.curiousconcept.com/ get an error.

get_all.php

<?php 

header('Content-Type: application/json; charset=utf-8');

?>

<?php


$response = array();

require 'db_connect.php';

$db = new DB_CONNECT();

$result = mysql_query("SELECT * FROM demo") or die(mysql_error());

if (mysql_num_rows($result) > 0) {

    $response["demo"] = array();

    while ($row = mysql_fetch_array($result)) {
        $product = array();
        $product["id"] = $row["id"];
        $product["name"] = $row["name"];
        $product["detaly"] = $row["detaly"];

        array_push($response["demo"], $product);
    }
    $response["success"] = 1;


    echo json_encode($response);

} else {
    $response["success"] = 0;
    $response["message"] = "No products found";

    echo json_encode($response);
}
?>

I've honestly spent so much time searching for this problem, but the answer is not found. All the same, why do I have prints to the database is not json? DB encoded utf_unicode_ci, and the table in utf8_general_ci

Upvotes: 0

Views: 88

Answers (2)

Gabriel
Gabriel

Reputation: 1449

There is an extraneous carriage return in front of your { } sequence (which is valid, in itself).

You should not close ?> and then reopen <?php your script after the header instruction. It outputs garbage to the browser. You really want your stream to begin with the { first character.

In other words (for @KnightRider) the lines 5-7 of the script should be removed!

05 ?>
06
07 <?php

Upvotes: 3

Knight Rider
Knight Rider

Reputation: 188

Sorry for answering this is because I could not attempt but have to write my views

Hi, I have checked this which you posted in the comment and JSONlint verifies it as a valid JSON

{"demo":[{"id":"3","name":"123123","detaly":"123123123"},{"id":"4","name":"4444‌​‌​","detaly":"555555"}],"success":1}

What else do you need?

Upvotes: 1

Related Questions