I am trying to convert my PHP array that i get as a query result to a JSON array. But look like it does not work. Any help would be appriciated.
$stmt = $db->prepare("SELECT * FROM companies WHERE companyID = ?");
if($stmt == "false"){
die('Query error !'.$db->error);
}
$stmt->bind_param('i', $companyID);
$stmt->execute();
$result = $stmt -> get_result();
$companies = $result ->fetch_all(MYSQLI_BOTH);
echo json_encode(array('companies' => $companies));
asked Sep 30, 2014 at 19:29
Tartar
5,49218 gold badges69 silver badges109 bronze badges
1 Answer 1
It looks like what you're experiencing is JSON itself. There is an existing, excellent answer here that will explain that. The summary is that an array with string keys in PHP is an object in JSON, since arrays can only have numeric indexes.
I hope this answers your question directly enough, and I think it will change how you think of the relationship between PHP and JSON.
Sign up to request clarification or add additional context in comments.
Comments
lang-php
But look like it does not work.please be even less specific if possible$result ->fetch_allactually give you an array or just something array-like?echo json_encode(array('companies' => $companies));?