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"
]
]
}