I have done a little research on the following error Warning: [json] (php_json_encode) type is unsupported, encoded as nullbut have not found much in the way of answers?
I am trying to encode the result of a mysql query. Not sure what info I can provide.. but when I echo the results of the mysql data using
$data = json_encode($result);
echo $data;
while($info = mysql_fetch_array($result))
{
$content[] = $info;
}
$count = count($content);
for($i=0;$i<$count;$i++){
echo $content[$i]['Name'];
}
every thing shows as normal. Any help would be great.
What about:
$result=array();
for($i=0;$i<$count;$i++)
{
$result[]=$content[$i]['Name'];
}
echo json_encode($result);
mysql_fetch_array will return an array with both numeric and non-numeric values. That's fine for PHP, but it doesn't really play as well in other formats and it could very well be the cause of the issue. Have you tried mysql_fetch_assoc?
BIG EDIT
Just noticed your code above. You have :
$data = json_encode($result);
echo $data;
while($info = mysql_fetch_array($result))
That won't work. $result is a resource data type. It can't be serialized into something which can be read by json_encode. You have to create an array of its contents first and serialize that. Just making sure you know.
Related
I am like to view the data retrieved by the SQL statement but when doing it only
json_response display the data but what I want to see is echo json_encode($json_response) format of data.
I follow this tutorial but did not get the same result.
json_encode($json_response) shows no data AKA plain screen
http://www.webslesson.info/2016/05/convert-data-from-mysql-to-json-formate-using-php.html
$query = "SELECT * from LibraryEvents";
$result = mysqli_query($connDB,$query);
$json_response = array();
while($row = mysqli_fetch_assoc($result)) {
$json_response[] = $row;
//array_push($json_response,$row_array);
}
echo '<pre>';
print_r($json_response);
echo '<pre>';
echo json_encode($json_response);
Frequenty, this is due to your response having accents: if your response has accents (é, à, etc.), json_encode will not work properly.
This might help you: How to json_encode array with french accents?
I am new to php, for my android application I am using php. My problem is I can not able to access the array object value.
Store the select query table value like below
$rows = array();
$i=0;
while($r = mysqli_fetch_assoc($result)) {
$rows[$i] = $r;
$i++;
}
When I display the value using echo json_encode($rows); it will display [{"CurrentVersion":"0.0.1","ForceUpdate":"1"}] this value. But when I trying to access this value in php I can not able to access I tried the below methods.
echo $verDetails[0].ForceUpdate; => ArrayForceUpdate <- I got this value
echo $verDetails[0] => Array <- I got this value
echo $verDetails[0]->ForceUpdate;=> Nothing got
I want to get the Forceupdate value = 1. Please help me, I know many one feel this is the cheap question and you will put negative vote, but I don't have other option. I tried in many ways but I am not get any answers.
mysqli_fetch_assoc returns an associative array. So you have to access the values like this:
$verDetails[0]['ForceUpdate']
To use the arrow syntax, use mysqli_fetch_object:
while($r = mysqli_fetch_object($result)) {
$rows[$i] = $r;
$i++;
}
You have multidimensional array try like this..
echo $rows[0]['CurrentVersion'];
echo $rows[0]['ForceUpdate'];
If you have json format data.use json_decode() to convert it into array.Then access each element.like this
$json = '[{"CurrentVersion":"0.0.1","ForceUpdate":"1"}]';
$rows = json_decode($json,true);
echo $rows[0]['CurrentVersion']."<br/>";
echo $rows[0]['ForceUpdate'];
So I have this PHP script that let me get photo objects from MySQL database, while fetching the results with mysql_fetch_array() function I push the row into an array. Which works, simple and good.
After the while, I do an echo of the array size and it does work too.
Then, when I try to encode the array to json format and I test it I get "Response does not contain any data" with a Ok status from Chrome's Advanced Rest Client.
if (mysql_num_rows($result) > 0)
{
// looping through all results
// photo node
$response["photos"] = array();
while ($row = mysql_fetch_array($result)) {
// temp photo array
$photo = array();
$photo["photoid"] = $row["photoid"];
$photo["photodescription"] = $row["photodescription"];
$photo["uploaderid"] = $row["uploaderid"];
$photo["takenat"] = $row["takenat"];
$photo["nblikes"] = $row["nblikes"];
$photo["photourl"] = $row["photourl"];
$photo["thumbnailurl"] = $row["thumbnailurl"];
// push single photo into final response array
array_push($response["photos"], $photo);
}
// success
$response["success"] = 1;
// echoing JSON response
echo sizeof($response["photos"]);
echo json_encode($response);
}
Can anyone help, please ?
Thanks to RonnySkansing, it appeared that I had an error while encoding. Which is a "JSON_ERROR_UTF8: Malformed UTF-8 characters, possibly incorrectly encoded"
I've added then this : mysql_set_charset("utf8");
And it's done.
Voila.
Your select probably has an error or the number of rows is 0. Try printing the numbers of rows.
echo mysql_num_rows($result);
i have done this code in php and it fetches every detail correctly apart from the images it shows "product_photo":null while there is an image.
any ideas why is null?
while ($row = mysql_fetch_object($qry_result)){
$result[] = $row;
}
$jsonstring = json_encode($result);
echo $jsonstring;
I'm not familiar with PHP and JSON but need it for my Android project.
I'm confused about which JSON format is the most effective. Most examples I saw use Associative array by using this code:
$result = mysql_query($queryString)
while($row = mysql_fetch_assoc($result)){
$json['contact']['ID'] = $row['ID'];
$json['contact']['Name'] = $row['Name'];
}
Format #1
{"contact":{"ID":"1","Name":"Andy"}}
I like that format but I don't know how to make each key hold more than one value. So when my query returns Andy and Bob, the json will only contains Bob.
Then, I found this PHP code:
while($row = mysql_fetch_row($result)){
$json['contact'][] = $row;
}
echo json_encode($json);
It makes the json looks like this:
Format #2
{"contact":[["1","Andy"],["2","Bob"]]}
But in Android, there is no JSONObject method to getArray(). Even if there is, I want to avoid using numbered array that requires [0] or [1] to be called.
So, which one is better? Or any other alternative?
Thanks
It's not that one method is more "effective" than the other, they serve different purposes. One is an associative object, and the other is a indexed array.
In your case, it looks like you want a list (indexed array) of objects. So what i would recommend doing is:
$result = mysql_query($queryString)
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$json['contact'][] = $row;
}
echo json_encode($json);
which would result in the json:
{"contact":[{"ID":"1","Name":"Andy"}, {"ID":"2","Name":"Bob"}]}
Why not use mysql_fetch_array(); ?
{"contacts":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}
An object with key contacts that contain an array of contact objects perhaps?
You can also do it hardcoded (not as nice as mysql_fetch_array() tho.);
$result = array();
$i = 0;
while(...) {
$result[] .= '{"id":"'.$i.'","name":"'.$fetch['name'].'"}';
$i++;
}
echo "[".implode(',', $result)."]";