2

I have been trying to echo json array data, but I am getting error. I have not done it before. I want to use it for datatable.

Here is what I have tried to do, but it shows error from, but if I copy the return text and save it in php folder it works fine. Please I need help to fix it.

<?php
try {
    $db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
    $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $users_stmt = $db_conn->prepare("SELECT * FROM users");
    $users_stmt->execute();
    while ($row = $users_stmt->fetch(PDO::FETCH_ASSOC)){
        //json_encode($users_stmt->fetch(PDO::FETCH_ASSOC));
        $dataarray = '
            [
                "'.$row['username'].'",
                "'.$row['fullname'].'",
                "'.$row['email'].'",
                "'.$row['siteright'].'",
                "2011/04/25",
                "$320,800"
            ],
        ';

    }
} catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage();}

echo '{<br/>
    "data": [<br/>'
    .$dataarray.
    '<br/>[
      "Donna Snider",
      "Customer Support",
      "New York",
      "4226",
      "2011/01/25",
      "$112,000"
    ]<br/>
  ]<br/>
}';
?>

I want it to look like this

{
  "data": [
    [
      "Tiger Nixon",
      "System Architect",
      "Edinburgh",
      "5421",
      "2011/04/25",
      "$320,800"
    ],
    [
      "Donna Snider",
      "Customer Support",
      "New York",
      "4226",
      "2011/01/25",
      "$112,000"
    ]
  ]
}
1
  • $dataarray=array(); while ($row = $users_stmt->fetch(PDO::FETCH_ASSOC)){ $dataarray[] = $row; } echo '<pre>'; echo json_encode($dataarray); echo '</pre>'; Commented Sep 2, 2016 at 11:03

3 Answers 3

3

reference link JSON_PRETTY_PRINT

while ($row = $users_stmt->fetch(PDO::FETCH_ASSOC)){
$dataarray['data'][] = array(
                       $row['username'], $row['fullname'], 
                       $row['email'], $row['siteright'], 
                       2011/04/25, $320,800
                     );
}

 $json_data = json_encode($newData,JSON_PRETTY_PRINT);  

 echo $json_data;
Sign up to request clarification or add additional context in comments.

1 Comment

If you're sending $json_data to an area inside HTML tags, you can put the data between <pre> tags so the whitespaces aren't ignored. This way, you can see the formatting you put together using JSON_PRETTY_PRINT. Example: echo "<pre>$jsonData</pre>"; .
1

I think you want to do in this way -

while ($row = $users_stmt->fetch(PDO::FETCH_ASSOC)){
  $dataarray['data'][] = array(
                           $row['username'], $row['fullname'], 
                           $row['email'], $row['siteright'], 
                           2011/04/25, $320,800
                         );
}
$jsonData = json_encode($dataarray);

print_r($jsonData); // your json output

Comments

0
echo '<pre>';
print_r($jsonData);     //  instead of echo
echo '</pre>';

2 Comments

i still get this error DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
I guess is an AJAX response, then remove <pre> tags, whole document has to start with { and end with } without any breaks etc. then you can use echo with JSON string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.